From e4f6d41cd09e97f6b80908a8ef0f7dce231f7150 Mon Sep 17 00:00:00 2001 From: jailannashaat Date: Sun, 11 Feb 2024 00:42:55 +0200 Subject: [PATCH] new login with jwt plus the grant task --- src/app.js | 1 + src/controllers/user.js | 39 ++++++++++++++++++++++++++++----------- src/models/teamModel.js | 13 +++++++++++++ src/models/userModel.js | 11 +++++++++++ src/routes/user.js | 3 ++- 5 files changed, 55 insertions(+), 12 deletions(-) diff --git a/src/app.js b/src/app.js index 591ca25..2a8c9c9 100644 --- a/src/app.js +++ b/src/app.js @@ -14,6 +14,7 @@ app.use(bodyParser.urlencoded({ extended: true })); app.use('/',login); app.use('/', register); +app.use('/', grant); app.get("/", (req, res) => { res.send("Response OK"); diff --git a/src/controllers/user.js b/src/controllers/user.js index 0c48a54..b5f11ac 100644 --- a/src/controllers/user.js +++ b/src/controllers/user.js @@ -1,14 +1,20 @@ -export const loginController = (req,res) => { - const ref_email = "1234@gmail.com" - const ref_pass = "1234"; - let {email, pass } = req.body; - if (email === ref_email && pass == ref_pass) - { - res.send('true\n'); - } - else{ - res.send('entre valid email and password\n') - } +import { memberExist, updateMember } from "../models/teamModel"; +import { getUser, userRole } from "../models/userModel"; + +export const loginController = async (req,res) => { + const { username, email, password } = req.body; + const User = await getUser(email); + + if (User) { + const token = jwt.sign({ email: User.email }, 'averysecretkey', { expiresIn: '2h' }); + res.json({ token }); + + const role= await userRole(email); + console.log(`User's role: ${role}`); + + } else { + res.status(401).json({ message: 'Invalid username or password' }); + } } @@ -23,4 +29,15 @@ export const registerController = (req, res) => { users.push({ email, username, password }); res.status(201).send('User registered successfully.'); +}; + +export const grantController = async (req, res) => { + const { team_id, user_id} = req.body; + const existingMember = await memberExist(team_id, user_id); + + if (!existingMember) { + console.log('No team member with the given user id'); + } + await updateMember(team_id, user_id); + console.log('Successfully updated the role'); }; \ No newline at end of file diff --git a/src/models/teamModel.js b/src/models/teamModel.js index a8dd0b0..3d71a34 100644 --- a/src/models/teamModel.js +++ b/src/models/teamModel.js @@ -97,3 +97,16 @@ export async function deleteTeam (team_id) { } }) } + +export async function updateMember(team_id, user_id) { + await prisma.teamMembers.update({ + where: { + teamId_userId: { + teamId: team_id, + userId: user_id.id + }, + role:'LEADER' + } + }) + return true + } \ No newline at end of file diff --git a/src/models/userModel.js b/src/models/userModel.js index a644e94..b2a1e4a 100644 --- a/src/models/userModel.js +++ b/src/models/userModel.js @@ -72,3 +72,14 @@ export async function updateUser (e_mail, userData) { } return false } +export async function userRole (e_mail) { + const user_Role = await prisma.user.findUnique({ + where: { + email: e_mail + }, + select: { + role: true + } + }) + return user_Role +} \ No newline at end of file diff --git a/src/routes/user.js b/src/routes/user.js index e00f96e..4c855e7 100644 --- a/src/routes/user.js +++ b/src/routes/user.js @@ -1,8 +1,9 @@ import express from "express"; -import { loginController, registerController } from '../controllers/user.js'; +import { loginController, registerController, grantController } from '../controllers/user.js'; const route = express.Router(); route.post('/login',loginController); route.post('/register', registerController); +route.post('/grant', grantController); export default route; \ No newline at end of file