From 3824499433a455694c7438b22825ba33f833c935 Mon Sep 17 00:00:00 2001 From: kamadi2000 Date: Tue, 10 Oct 2023 11:00:42 +0530 Subject: [PATCH] google login direct to dashboard --- backend/package.json | 1 + backend/src/controllers/authController.js | 21 ++++++++++----------- webapp/src/components/GoogleLoginButton.js | 5 ++++- webapp/src/views/Login.js | 7 +++++-- 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/backend/package.json b/backend/package.json index 2cd802a..f7ab3bb 100644 --- a/backend/package.json +++ b/backend/package.json @@ -4,6 +4,7 @@ "description": "", "main": "index.js", "scripts": { + "start": "nodemon server.js", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], diff --git a/backend/src/controllers/authController.js b/backend/src/controllers/authController.js index 87df675..1ab51ce 100644 --- a/backend/src/controllers/authController.js +++ b/backend/src/controllers/authController.js @@ -88,6 +88,7 @@ exports.login = async (req, res) => { exports.loginApp = async (req, res) => { try { const { email, password } = req.body; + console.log({ email, password}) // Check if the username exists in the database const user = await User.findOne({ email }); @@ -101,7 +102,7 @@ exports.loginApp = async (req, res) => { if (passwordMatch) { const token = jwt.sign({ name: user.name, isVerified: user.isVerified }, process.env.SECURITY_KEY, { expiresIn: '5hour' }); - return res.header("x-auth-token", token).status(201).json({token}); + return res.header("x-auth-token", token).status(201).json({token, success : true}); } else { console.log("Invalid credentials.", user.email); @@ -115,6 +116,7 @@ exports.loginApp = async (req, res) => { exports.signUpApp = async (req, res) => { try { + const otp = OTPGenerator() // Extract user information const { email, password, name } = req.body; const user = await User.findOne({ email }) @@ -127,17 +129,13 @@ exports.signUpApp = async (req, res) => { const user = new User({ email, hashedPassword, name }); await user.save(); - const token = jwt.sign({ name: user.name, isVerified: user.isVerified }, process.env.SECURITY_KEY, { expiresIn: '5hour' }); - return res.header("x-auth-token", token).status(201).json({token}); - } - - - + await emailModule.sendOTP(email, otp) - - - + const token = jwt.sign({ name: user.name, isVerified: user.isVerified }, process.env.SECURITY_KEY, { expiresIn: '5hour' }); + return res.header("x-auth-token", token).status(201).json({token, success : true}); + + } } catch (error) { console.log('hi') console.error(error); @@ -184,6 +182,7 @@ const googleLoginBase = async (req, res, isWeb) => { await newUser.save() // Send the response + newUser.isVerified = true const newToken = jwt.sign({ name: newUser.name, isVerified: newUser.isVerified }, process.env.SECURITY_KEY, { expiresIn: '5hour' }); if(isWeb){ res.cookie("token", newToken,{ maxAge: 900000, httpOnly: true }); @@ -196,7 +195,7 @@ const googleLoginBase = async (req, res, isWeb) => { } - + user.isVerified = true const newToken = jwt.sign({ name: user.name, isVerified: user.isVerified }, process.env.SECURITY_KEY, { expiresIn: '5hour' }); diff --git a/webapp/src/components/GoogleLoginButton.js b/webapp/src/components/GoogleLoginButton.js index 2c98381..d1e1bcd 100644 --- a/webapp/src/components/GoogleLoginButton.js +++ b/webapp/src/components/GoogleLoginButton.js @@ -2,16 +2,19 @@ import React, { useState, useEffect } from 'react'; import { GoogleLogin, useGoogleLogin } from '@react-oauth/google'; import axios from 'axios'; import Button from 'react-bootstrap/Button'; +import { useNavigate } from 'react-router-dom'; function GoogleLoginButton() { + const navigate = useNavigate() const responseMessage = (response) => { axios.defaults.withCredentials = true axios.post('http://localhost:5000/api/auth/googleLogin', { token: response.access_token }) .then(response => { // Handle the successful response here console.log(response.data); - alert('login successful') + // alert('login successful') + navigate('/dashboard',{replace : true}) }) .catch(error => { // Handle any errors that occur during the request diff --git a/webapp/src/views/Login.js b/webapp/src/views/Login.js index 3d60671..fad5088 100644 --- a/webapp/src/views/Login.js +++ b/webapp/src/views/Login.js @@ -8,6 +8,7 @@ import { useAuthContext } from "../context/AuthContext"; import axios from 'axios'; import { getSessionCookie } from "../utils/cookie"; import jwt_decode from 'jwt-decode' +import GoogleLoginButton from "../components/GoogleLoginButton"; export default function Login() { const navigate = useNavigate() @@ -95,7 +96,9 @@ export default function Login() {

OR

*/}

OR

- + */}