-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #241 from SaurabhS55/googleAuth
Feat#82: Google Authentication, Login and Signup backend
- Loading branch information
Showing
16 changed files
with
402 additions
and
217 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
VITE_BACKEND_URL=http://localhost:7000/api/v1 | ||
VITE_GOOGLE_CLIENT_ID=430349486581-523p7m5mlh1mjjne5h3h3q2t0hd0okeg.apps.googleusercontent.com |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
|
||
./package-lock.json | ||
./package-lock.json | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,4 @@ const connectDB = async () => { | |
} | ||
}; | ||
|
||
export default connectDB; | ||
export {connectDB}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import {userModel} from "../models/userModel.js"; | ||
import bcrypt from "bcrypt"; | ||
import jwt from "jsonwebtoken"; | ||
|
||
const postLogin=async (req,res)=>{ | ||
try { | ||
if(req.body.authType===true){ | ||
const user=await userModel.findOne({ email: req.body.email }); | ||
if(!user){ | ||
const hashedPassword = await bcrypt.hash(req.body.password, 10); | ||
const pass= req.body.password; | ||
req.body.password = hashedPassword; | ||
const data = await userModel.create({ | ||
email:req.body.email, | ||
password:req.body.password | ||
}); | ||
req.body.password=pass | ||
} | ||
} | ||
const currentUser = await userModel.findOne({ email: req.body.email }); | ||
if (currentUser) { | ||
const isPasswordCorrect = bcrypt.compareSync( | ||
req.body.password, | ||
currentUser.password | ||
); | ||
if (isPasswordCorrect) { | ||
const token = jwt.sign( | ||
{ id: currentUser._id }, | ||
process.env.JWT_SECRET | ||
); | ||
res.cookie('jwttoken', token, { | ||
httpOnly: false, | ||
maxAge: 24 * 60 * 60 * 10 | ||
}); | ||
return res.status(200).json({ message: "Login Successfully"}); | ||
} else { | ||
return res.status(400).json({ message: "Password is Incorrect" }); | ||
} | ||
} | ||
else { | ||
return res.status(400).json({ message: "User not found" }); | ||
} | ||
} | ||
catch (err) { | ||
res.status(500).json({ message:"server error" }); | ||
} | ||
} | ||
export { postLogin }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import {userModel} from "../models/userModel.js"; | ||
import bcrypt from "bcrypt"; | ||
const postRegister = async (req, res, next) => { | ||
try { | ||
const hashedPassword = await bcrypt.hash(req.body.password, 10); | ||
req.body.password = hashedPassword; | ||
const data = await userModel.create({ | ||
...req.body, | ||
}); | ||
console.log(data); | ||
res.status(201).json({message: "User Created"}); | ||
} catch (err) { | ||
// console.log(err); | ||
res.status(400).json({ message: err.message }); | ||
} | ||
}; | ||
export { postRegister }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import jwt from "jsonwebtoken"; | ||
import { userModel } from "../models/userModel.js"; | ||
import bcrypt from "bcrypt"; | ||
|
||
const authLogin = async (req, res, next) => { | ||
try { | ||
const token = req.cookies.jwttoken; | ||
const verifyToken = jwt.verify(token, process.env.jwt_secret); | ||
const user = await userModel.findOne({ _id: verifyToken.id }); | ||
if(!user){ | ||
return res.status(401).json({ message: "User not found" }); | ||
} | ||
else{ | ||
next(); | ||
} | ||
} | ||
catch (err) { | ||
res.status(501).json({ message: "Internal Server Error" }); | ||
} | ||
} | ||
|
||
export { authLogin }; |
Oops, something went wrong.