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
80 changes: 80 additions & 0 deletions backend/authintication/signup_login.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { GraphQLClient} from "graphql-request";
import jwt from 'jsonwebtoken';
import { Query } from "../graphql_queris/query.mjs";
import bodyParser from "body-parser";

const gph_query = new Query();

const hasuraEndpoint = 'https://willing-gull-77.hasura.app/v1/graphql';
const adminSecret = 'CK8Kys28AWtFWGz78Vk0WGX6BAZCD4GetlpOQvRKxRsWMdVZBOM2H3pFtjp7kIey';

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 AuthLogic{


async insertSignupUser(req, res){
try{
// console.log(req.body)
const {first_name, last_name, email, password, company_name, company_role} = req.body;
const data = await client.request(gph_query.signupInsertUser(first_name, last_name, email, password, company_name, company_role),{});
// console.log(data);
// res.json(data);
res.status(200).json(
{
"uuid":data.insert_kalenview_one.uuid,
"first_name":data.insert_kalenview_one.first_name,
"last_name":data.insert_kalenview_one.last_name
});
}catch (error) {
console.error('Error fetching data:', error);
res.status(500).json({error: 'Internal server error'});
}
}


async login(req, res){
try{
// console.log(req.body)
const {email, password} = req.body;
const data = await client.request(gph_query.loginUser(email, password),{});
// 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
});
}catch (error) {
console.error('Error fetching data:', error);
res.status(500).json({error: 'Kindly Sign Up or Check email and password'});
}
}


}
29 changes: 29 additions & 0 deletions backend/graphql_queris/query.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export class Query{

signupInsertUser(first_name, last_name, email, password, company_name, company_role){
return `
mutation MyMutation {
insert_kalenview_one(object: {company_name: "${company_name}", company_role: "${company_role}",
email: "${email}", first_name: "${first_name}", last_name: "${last_name}", password: "${password}"}) {
uuid
first_name
last_name
}
}
`
};

loginUser(email, password){
return `
query MyQuery {
kalenview_by_pk(email: "${email}", password: "${password}") {
uuid
first_name
last_name
}
}
`
};


}
14 changes: 14 additions & 0 deletions backend/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import express from 'express';
import { router } from './routes/routes.mjs';

const app = express();
const PORT = process.env.PORT || 6541;

app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(router);


app.listen(PORT, ()=>{
console.log(`URL: http://localhost:${PORT}`);
});
Loading