Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var passwordHash = require('password-hash');
- var jwt = require('jsonwebtoken');
- module.exports = (app, db) => {
- /* GET home page. */
- app.post('/api/user/login', function (req, res) {
- console.log(req.body);
- var hashedPassword = passwordHash.generate(req.body.password);
- db.User.findAll({
- where: {
- email: req.body.email
- },
- include: [db.Company]
- }).then(function (users) {
- if (users.length > 0) {
- if (users[0].roleid == 2)
- {
- db.Enroller.findAll({
- where:{email:req.body.email}
- }).then(function(enrollers){
- if(enrollers.length > 0)
- {
- var passwordcheck = passwordHash.verify(req.body.password, users[0].password);
- if (passwordcheck) {
- // res.send({ id:users[0].id,username:users[0].username});
- var token = jwt.sign({ user: users[0] }, "mysecret", {
- expiresIn: "24h" // expires in 24 hours
- });
- console.log(token);
- // return the information including token as JSON
- res.json({
- code: 200,
- success: true,
- message: 'Logged in successfully.',
- Data: users[0],
- LoginId:enrollers[0].id,
- email:enrollers[0].email,
- token: token
- });
- }
- else
- res.json({
- code: 300,
- success: true,
- message: 'Email/Password combination did not match. Please try again.',
- Data: ""
- });
- }
- else{
- res.json({
- code: 300,
- success: true,
- message: 'User not found. Please re-enter correct credentials.',
- Data: ""
- });
- }
- })
- }
- else
- {
- var passwordcheck = passwordHash.verify(req.body.password, users[0].password);
- if (passwordcheck) {
- var token = jwt.sign({ user: users[0] }, "mysecret", {
- expiresIn: "24h" // expires in 24 hours
- });
- res.json({
- code: 200,
- success: true,
- message: 'Logged in successfully.',
- Data: users[0],
- LoginId:users[0].id,
- email:users[0].email,
- token: token
- });
- }
- else {
- res.json({
- code: 300,
- success: true,
- message: 'Email/Password combination did not match. Please try again.',
- Data: ""
- });
- }
- }
- }
- else
- res.json({
- code: 300,
- success: true,
- message: 'Username not found!',
- Data: ""
- });
- });
- });
- app.post('/api/user/registration', function (req, res) {
- var hashedPassword = passwordHash.generate(req.body.password);
- db.User.findAll({
- where: {
- username: req.body.username
- }
- }).then(function (users) {
- if (users.length > 0) {
- res.json({
- code: 300,
- success: true,
- message: 'Already Registered!',
- Data: users
- });
- }
- else {
- db.Company.findAll(
- {
- where: { id: req.body.company_id }
- }
- ).then(function (company) {
- if (company.length > 0) {
- db.User.build({
- firstname: req.body.firstname,
- lastname: req.body.lastname,
- username: req.body.username,
- email: req.body.email,
- password: hashedPassword,
- last_login: req.body.last_login,
- roleid: req.body.roleid,
- status: req.body.status,
- company_id: company[0].id,
- Company: company[0]
- }, {
- include: [
- {
- model: db.Company,
- as: 'company'
- }
- ]
- }
- )
- .save()
- .then(function (data) {
- res.json({
- code: 200,
- success: true,
- message: 'Successfully registered!',
- Data: data
- });
- })
- .catch(error => {
- res.json({
- code: 500,
- success: false,
- message: 'Registration failed.',
- Data: error
- });
- // Ooops, do some error-handling
- });
- }
- });
- }
- });
- });
- }
- // 200 - OK
- // 404 - Not Found
- // 500 - Internal Server Error
- // If you're not comfortable reducing all your error conditions to these 3, try picking among these additional 5:
- // 201 - Created
- // 300- status message
- // 304 - Not Modified
- // 400 - Bad Request
- // 401 - Unauthorized
- // 403 - Forbidden
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement