-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
edc8088
commit a8df507
Showing
7 changed files
with
354 additions
and
0 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,70 @@ | ||
import bcrypt from "bcryptjs"; | ||
import { User } from "../models/user.auth.js"; | ||
|
||
export const Signup = async (req, res) => { | ||
try { | ||
const { fullName, userName, password, confirmPassword, gender } = req.body; | ||
|
||
if (password !== confirmPassword) { | ||
console.log("password variation"); | ||
res.status(500).send({ error: "Password doesn't match confirmation" }); | ||
} | ||
|
||
const user = await User.findOne({ userName }); | ||
if (user) { | ||
console.log("Duplicate username"); | ||
res.status(500).send({ error: "Duplicate user" }); | ||
} | ||
|
||
const boyProfile = `https://avatar.iran.liara.run/public/boy?username=${userName}`; | ||
const girlProfile = `https://avatar.iran.liara.run/public/girl?username=${userName}`; | ||
|
||
const newUser = await new User({ | ||
fullName, | ||
userName, | ||
password, | ||
gender, | ||
profilePic: gender === "male" ? boyProfile : girlProfile, | ||
}); | ||
|
||
newUser | ||
.save() | ||
.then((result) => { | ||
res.status(201).json({ | ||
_id: newUser._id, | ||
fullName: newUser.fullName, | ||
userName: newUser.userName, | ||
gender: newUser.gender, | ||
}); | ||
}) | ||
.catch((err) => { | ||
console.log(err); | ||
}); | ||
res.redirect("/auth/login"); | ||
} catch (err) { | ||
console.log(err.message); | ||
res.status(500).send({ error: "Internal server error" }); | ||
} | ||
}; | ||
|
||
export const Login = async (req, res) => { | ||
try { | ||
const { userName, password } = req.body; | ||
const user = User.findOne({ userName }); | ||
const isPassword = await bcrypt.compare(password, user.password || ""); | ||
if (!user && !isPassword) { | ||
res.status(400).json({ error: "Invalid username or password" }); | ||
console.log("Invalid password or username"); | ||
} | ||
generateTokensAndSetCookies(user._id, res); | ||
res.status(201).json({ | ||
_id: user._id, | ||
fullName: user.fullName, | ||
userName: user.userName, | ||
gender: user.gender, | ||
profilePic: user.profilePic, | ||
}); | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
}; |
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,10 @@ | ||
import mongoose from "mongoose"; | ||
|
||
export const authConn = async () => { | ||
try { | ||
await mongoose.connect(process.env.MONGO_URI); | ||
console.log("Mongoose connected."); | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
}; |
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,29 @@ | ||
import mongoose from "mongoose"; | ||
|
||
const authSchema = mongoose.Schema({ | ||
fullName: { | ||
type: String, | ||
required: true, | ||
}, | ||
userName: { | ||
type: String, | ||
required: true, | ||
unique: true, | ||
minLength: 3, | ||
}, | ||
password: { | ||
type: String, | ||
required: true, | ||
minLength: 4, | ||
}, | ||
gender: { | ||
type: String, | ||
enum: ["male", "female"], | ||
}, | ||
profilePic: { | ||
type: "String", | ||
default: "", | ||
}, | ||
}); | ||
|
||
export const User = mongoose.model("User", authSchema); |
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,8 @@ | ||
import express from "express"; | ||
import { Signup, Login } from "../controllers/user.auth.js"; | ||
|
||
export const router = express.Router(); | ||
|
||
router.post("/signup", Signup); | ||
|
||
router.post("/login", Login); |
Oops, something went wrong.