Skip to content

Commit

Permalink
google login direct to dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
kamadi2000 committed Oct 10, 2023
1 parent af67de1 commit 3824499
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon server.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
Expand Down
21 changes: 10 additions & 11 deletions backend/src/controllers/authController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand All @@ -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);
Expand All @@ -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 })
Expand All @@ -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);
Expand Down Expand Up @@ -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 });
Expand All @@ -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' });


Expand Down
5 changes: 4 additions & 1 deletion webapp/src/components/GoogleLoginButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions webapp/src/views/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -95,7 +96,9 @@ export default function Login() {
<p>OR</p> */}
<p className="hr-line"><span>OR</span></p>

<Button type="submit" className="mb-3 task-button" style={{ display: 'flex', flexDirection: 'row', justifyContent: 'center', alignItems: 'center' }}>
<GoogleLoginButton/>

{/* <Button type="submit" className="mb-3 task-button" style={{ display: 'flex', flexDirection: 'row', justifyContent: 'center', alignItems: 'center' }}>
<div style={{ flex: 1 / 3, textAlign: 'start' }}>
<i className="bi bi-google"></i>
</div>
Expand All @@ -104,7 +107,7 @@ export default function Login() {
</div>
</Button>
</Button> */}
<Button className="mb-3 task-button" style={{ display: 'flex', flexDirection: 'row', justifyContent: 'center', alignItems: 'center' }}>
<div style={{ flex: 1 / 3, textAlign: 'start' }}>
<i className="bi bi-facebook"></i>
Expand Down

0 comments on commit 3824499

Please sign in to comment.