Skip to content

Commit 12ed153

Browse files
author
sourabh
committed
[auth]-work on signin,signup,logout
1 parent f29b2c4 commit 12ed153

File tree

10 files changed

+248
-16
lines changed

10 files changed

+248
-16
lines changed

app.js

+8
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ app.use(
2525
})
2626
);
2727

28+
// TEMP CHRCK (EJS)
29+
app.set("view engine", "ejs");
30+
2831
//REGULAR MIDDLEWARE
2932
app.use(express.json());
3033
app.use(express.urlencoded({ extended: true }));
@@ -37,4 +40,9 @@ const user = require("./routes/user");
3740
app.use("/api/v1", home);
3841
app.use("/api/v1", user);
3942

43+
// IMAGE UPLOAD SECTION (EJS)
44+
app.get("/signuptest", (req, res) => {
45+
res.render("signuptesh");
46+
});
47+
4048
module.exports = app;

controllers/user.js

+51-8
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@ const cookieToken = require("../utils/cookieToken");
55
const fileUpload = require("express-fileupload");
66
const cloudinary = require("cloudinary");
77

8+
// SIGNUP
89
exports.signup = BigPromise(async (req, res, next) => {
910
// UPLOAD IMAGE
10-
let result;
11-
if (req.files) {
12-
let file = req.files.photo;
13-
result = await cloudinary.v2.uploader.upload(file, {
14-
folder: "users",
15-
width: 150,
16-
crop: "scale",
17-
});
11+
if (!req.files) {
12+
return next(new CustomError("Photo is required for signup", 400));
1813
}
1914

15+
let file = req.files.photo;
16+
17+
const result = await cloudinary.v2.uploader.upload(file.tempFilePath, {
18+
folder: "users",
19+
width: 150,
20+
crop: "scale",
21+
});
22+
2023
// INVITE FIELD
2124
const { name, email, password } = req.body;
2225

@@ -38,3 +41,43 @@ exports.signup = BigPromise(async (req, res, next) => {
3841

3942
cookieToken(user, res);
4043
});
44+
45+
// SIGNIN
46+
exports.login = BigPromise(async (req, res, next) => {
47+
const { email, password } = req.body;
48+
49+
// CHECK FIELDS
50+
if (!email || !password) {
51+
return next(
52+
new CustomError("Invalid Credentials,both field required", 400)
53+
);
54+
}
55+
56+
// !isEXITS ("+password").select("true") DB CHECK
57+
const user = await User.findOne({ email }).select("+password");
58+
59+
if (!user) {
60+
return next(new CustomError("Invalid Credentials,User Doesn't Exits", 400));
61+
}
62+
// CHECK !is_PASSWORD (MATCH)
63+
const isPasswordCorrect = await user.isValidatedPassword(password);
64+
65+
if (!isPasswordCorrect) {
66+
return next(new CustomError("Email & Password Doesn't Match", 400));
67+
}
68+
69+
// IF All GOES GOOD SEND TOKEN
70+
cookieToken(user, res);
71+
});
72+
73+
// LOGOUT
74+
exports.logout = BigPromise(async (req, res, next) => {
75+
res.cookie("token", null, {
76+
expires: new Date(Date.now()),
77+
httpOnly: true,
78+
});
79+
res.status(200).json({
80+
success: true,
81+
message: "LOGOUT SUCCESSFUL",
82+
});
83+
});

index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ const cloudinary = require("cloudinary");
66
// CONNECT TO DATABASE
77
connectWithDb();
88

9+
910
// CLOUDINARY CONFIG
1011
cloudinary.config({
11-
clound_name: process.env.CLOUDINARY_NAME,
12+
cloud_name: process.env.CLOUDINARY_NAME,
1213
api_key: process.env.CLOUDINARY_API_KEY,
13-
api_key: process.env.CLOUDINARY_API_SECRET,
14+
api_secret: process.env.CLOUDINARY_API_SECRET,
1415
});
1516

1617
app.listen(process.env.PORT, () => {

models/user.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const userSchema = new mongoose.Schema({
1919
password: {
2020
type: String,
2121
required: [true, "Please provide an password"],
22-
minlength: [6, "password should be atleast 6 char"],
22+
minlength: [4, "password should be atleast 4 char"],
2323
select: false,
2424
},
2525
role: {

package-lock.json

+128
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"cloudinary": "^1.37.2",
1919
"cookie-parser": "^1.4.6",
2020
"dotenv": "^16.3.1",
21+
"ejs": "^3.1.9",
2122
"express": "^4.18.2",
2223
"express-fileupload": "^1.4.0",
2324
"jsonwebtoken": "^9.0.0",

routes/home.js

+1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ const router = express.Router();
66
router.route("/").get(home)
77
router.route("/d").get(Dummy)
88

9+
910
module.exports = router;

routes/user.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
const express = require("express");
22
const router = express.Router();
3-
const {signup} = require("../controllers/user");
3+
const {signup, login, logout} = require("../controllers/user");
4+
45

56
router.route('/signup').post(signup);
7+
router.route('/login').post(login);
8+
router.route('/logout').get(logout);
9+
610

711
module.exports = router;

tracking_project/map_project.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/*
2+
@IMP:🔗✈️http://localhost:4000/api-docs/
3+
🔗✈️http://localhost:4000/signuptest
24
@NOTE: Create first "CustomError"(🗃️utils), "bigPromise"(🗃️middlewares) for Error Handling BEFOUR Start PROJECT;
35
46
@ABOUT_ROUTES:
@@ -151,14 +153,19 @@ Note: ALSO Allow PASSWORD Body itself
151153
-🎯Why Json for mobile perspective that's why you see easily token on web But.
152154
153155
Cookie token we need use frequently So create 🗃️utils/cookieToken/
154-
155-
156-
157-
158156
157+
-----------------------------SECTION
158+
IMAGE UPLOAD THEORY PENDING 🥱🥱🥱
159+
@VISITED FILE: index.js,.env, app.js CONTROLLER/user.js
160+
-----------------------------Then'll Come
159161
160162
163+
----------------------------------SECTION
164+
@LOCATION: 🗃️CONTROLLER/user.js
165+
@TITLE:->
166+
@ABOUT:->
161167
168+
@OVERVIEW@
162169
163170
164171
*/

0 commit comments

Comments
 (0)