Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion backend/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import express from 'express';
import cookieParser from 'cookie-parser';
import cors from 'cors';
import { router } from './routes/routes.mjs';

const app = express();
app.use(cors());
app.use(cookieParser());
app.use(cors({
origin: 'http://localhost:3000', // replace with your React app origin
credentials: true,
}));
const PORT = process.env.PORT || 6541;

app.use(express.json());
Expand Down
59 changes: 59 additions & 0 deletions backend/logics/Login/login.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { GraphQLClient} from 'graphql-request';
import { LoginQuery } from './query.mjs';
import jwt from 'jsonwebtoken';
// import bodyParser from "body-parser";

const login = new LoginQuery();

const hasuraEndpoint = 'http://localhost:8080/v1/graphql';
const adminSecret = '123';

const generateJwtToken = () => {
const claims = {
"sub": "1234567890",
"name": "Debesh Pramanick",
"https://hasura.io/jwt/claims": {
"x-hasura-allowed-roles": [
"admin",
"user"
],
"x-hasura-default-role": "admin"
// "x-hasura-default-role": "user"
}
}

return jwt.sign(claims, '5kOnr5vYmLggihää0X9TGrQBC4PoFPgt', { expiresIn: '1h' });
};

const client = new GraphQLClient(hasuraEndpoint, {
headers: {
'x-hasura-admin-secret': adminSecret
// 'Authorization': `Bearer ${generateJwtToken()}`
},
});



export class LoginUser{
async login(req, res){
try{
// console.log(req.body)
const {email, password} = req.body;
const data = await client.request(login.loginUser(email, password),{});
const token = generateJwtToken();
// console.log(data);
// res.json(data);
res.status(200).json(
{
"message":"Welcome User",
"uuid":data.kalenview_by_pk.uuid,
"first_name":data.kalenview_by_pk.first_name,
"last_name":data.kalenview_by_pk.last_name,
"token": token // Include the JWT in the response
});
}catch (error) {
console.error('Error fetching data:', error);
res.status(500).json({error: 'Kindly Sign Up or Check email and password'});
}
}
}
13 changes: 13 additions & 0 deletions backend/logics/Login/query.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export class LoginQuery {
loginUser(email, password){
return `
query MyQuery {
kalenview_by_pk(email: "${email}", password: "${password}") {
uuid
first_name
last_name
}
}
`
};
}
34 changes: 34 additions & 0 deletions backend/logics/VerifyJWT/verifyJWT.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const secretKey = '5kOnr5vYmLggihää0X9TGrQBC4PoFPgt';
import jwt from 'jsonwebtoken';

export class VerifyJWToken{
verifyJWT(req, res){
const token = req.headers.authorization;
// console.log(token)
// const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkRlYmVzaCBQcmFtYW5pY2siLCJodHRwczovL2hhc3VyYS5pby9qd3QvY2xhaW1zIjp7IngtaGFzdXJhLWFsbG93ZWQtcm9sZXMiOlsiYWRtaW4iLCJ1c2VyIl0sIngtaGFzdXJhLWRlZmF1bHQtcm9sZSI6ImFkbWluIn0sImlhdCI6MTcxNTg1NjgyOCwiZXhwIjoxNzE1ODYwNDI4fQ.MGQvAwpkbJlfmOZh9ki8ML2U0kJo69ijsFvn834dwb8';
try{
if (!token) {
console.log("No jwt");
return res.status(401).json({ success: false });

}

jwt.verify(token, secretKey, (err, decoded) => {
if (err) {
console.log("Invalid jwt");
return res.status(401).json({ success: false });
// return res.redirect('/login');
}

else{
console.log("Valid jwt");
return res.status(200).json({success:true});
}

});
}
catch(error){
res.json({message:error})
}
}
}
128 changes: 127 additions & 1 deletion backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{
"dependencies": {
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"express": "^4.19.2",
"graphql-request": "^7.0.1"
"graphql-request": "^7.0.1",
"jsonwebtoken": "^9.0.2"
},
"name": "backend",
"version": "1.0.0",
Expand Down
12 changes: 8 additions & 4 deletions backend/routes/routes.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,27 @@ import express from 'express';

import { CreateEvent } from '../logics/CreateEvent/createEvent.mjs';
import { GetEvent } from '../logics/GetEvents/getEvents.mjs';
import { LoginUser } from '../logics/Login/login.mjs';
import { VerifyJWToken } from '../logics/VerifyJWT/verifyJWT.mjs';

// const auth_logic = new AuthLogic();
// const norm_routes = new NormRoutes();
// const verify_jwt = new VerifyJwt();

const new_event = new CreateEvent();
const get_event = new GetEvent();
const login_user = new LoginUser();
const jwt_verify = new VerifyJWToken();
const router = express.Router();

// // For signup
// router.post('/signup', auth_logic.insertSignupUser);

// // For login
// router.post('/login', auth_logic.login);
// For login
router.post('/login', login_user.login);

// // For JWT Verification
// router.post('/verify', verify_jwt.verifyJwt);
// For JWT Verification
router.post('/verify', jwt_verify.verifyJWT);

// // For Home
// router.get('/', norm_routes.linkpage);
Expand Down
9 changes: 9 additions & 0 deletions frontend/frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"axios": "^1.6.8",
"js-cookie": "^3.0.5",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-icons": "^5.2.1",
Expand Down
Loading