-
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
Showing
14 changed files
with
859 additions
and
181 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
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,66 @@ | ||
import express from 'express'; | ||
import { handleValidation } from '../../middlewares/index'; | ||
import { createUser, checkUser, searchOne, createUserValidate } from '../../services/auth'; | ||
import dotenv from 'dotenv'; | ||
import jwt from 'jsonwebtoken'; | ||
|
||
const router = express.Router(); | ||
dotenv.config(); | ||
|
||
const createToken = (id, email) => | ||
jwt.sign({ id: id, email: email, exp: Math.floor(Date.now() / 1000) + parseInt(process.env.JWT_EXPIRES_IN) }, process.env.JWT_SECRET); | ||
|
||
const createUserHandler = async (req, res, next) => { | ||
try { | ||
const user = req.body; | ||
const userData = await createUser(user); | ||
if (userData) { | ||
res.status(201).send({ | ||
status: 'ok', | ||
message: 'User created successfully', | ||
result: userData, | ||
authToken: createToken(userData._id, userData.email), | ||
}); | ||
} else { | ||
throw new Error('User already exist') | ||
} | ||
} catch (error) { | ||
next(error); | ||
} | ||
}; | ||
|
||
const loginHandler = async (req, res) => { | ||
if (req.body.email && req.body.password) { | ||
let user = await checkUser(req.body.email, req.body.password); | ||
if (user) { | ||
res.status(200).send({ | ||
status: 'ok', | ||
result: { | ||
...user, | ||
}, | ||
authToken: createToken(user._id, user.email), | ||
}); | ||
return; | ||
} | ||
} | ||
|
||
res.status(400).send({ | ||
message: 'Invalid email or password ', | ||
}); | ||
return; | ||
}; | ||
|
||
const checkUserEmailHandler = async (req, res) => { | ||
const user = await searchOne({ username: req.body.username.toLowerCase() }); | ||
if (user) { | ||
res.status(400).send({ status: 'unavailable', message: 'Username is taken' }); | ||
return; | ||
} | ||
return res.status(200).send({ status: 'available', message: 'Username is available' }); | ||
}; | ||
|
||
router.post('/register', handleValidation(createUserValidate), createUserHandler); | ||
router.post('/login', loginHandler); | ||
// router.post('/check-username', handleValidation(validateUsername), checkUserEmailHandler); | ||
|
||
export default router; |
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
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,30 @@ | ||
import { Schema, model } from 'mongoose'; | ||
|
||
export interface IUser { | ||
firstName: string; | ||
lastName: string; | ||
email: string; | ||
password: string; | ||
isDelete?: boolean; | ||
} | ||
|
||
// schema | ||
|
||
const UserSceme = new Schema<IUser>( | ||
{ | ||
firstName: { type: String, required: true }, | ||
lastName: { type: String, required: true }, | ||
email: { type: String, require: true , unique : true }, | ||
password : {type : String}, | ||
isDelete: { | ||
type: Boolean, | ||
default: false, | ||
}, | ||
}, | ||
{ timestamps: true } | ||
); | ||
|
||
|
||
const User = model<IUser>('User', UserSceme); | ||
|
||
export default User; |
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
4 changes: 2 additions & 2 deletions
4
server/modules/member/index.ts → server/modules/members/index.ts
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,6 +1,6 @@ | ||
// const { authenticateRequest } = require("../../common/middlewares"); | ||
import memberControler from '../../controllers/member'; | ||
import memberControler from '../../controllers/members'; | ||
import request from '../../common/request'; | ||
export default request((app) => { | ||
app.use('/api/member', memberControler); | ||
app.use('/api/members', memberControler); | ||
}); |
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,5 @@ | ||
import userControler from '../../controllers/auth'; | ||
import request from '../../common/request'; | ||
export default request((app) => { | ||
app.use('/api/user', userControler); | ||
}); |
Oops, something went wrong.