-
Notifications
You must be signed in to change notification settings - Fork 6
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
28 changed files
with
427 additions
and
95 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,3 @@ | ||
DATABASE_URL=postgres://wmqxaqjr:tTHUvMlywe2owxdrqEaofzdC2RKF4XYp@raja.db.elephantsql.com:5432/wmqxaqjr | ||
DATABASE_TEST=postgres://wmqxaqjr:tTHUvMlywe2owxdrqEaofzdC2RKF4XYp@raja.db.elephantsql.com:5432/wmqxaqjr | ||
SECRET=qwertyuiop[]\';lkjhgfdsa`zxcvbnm,./+_)(*&^%$#@!) |
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 |
---|---|---|
@@ -1,10 +1,11 @@ | ||
# Logs | ||
logs | ||
*.log | ||
.dist | ||
.DS_Store | ||
|
||
npm-debug.log* | ||
|
||
seeders | ||
# Runtime data | ||
pids | ||
*.pid | ||
|
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,7 +1,7 @@ | ||
import path from 'path'; | ||
const path = require('path'); | ||
|
||
module.exports = { | ||
"config": path.resolve('./config', 'config.js'), | ||
"config": path.resolve('./config', 'index.js'), | ||
"models-path": path.resolve('./models'), | ||
"migrations-path": path.resolve('./migrations') | ||
}; |
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,7 +1,11 @@ | ||
language: node_js | ||
node_js: | ||
node_js: | ||
- "stable" | ||
services: | ||
- postgresql | ||
before_script: | ||
- psql -c 'CREATE DATABASE users_test;' -U postgres | ||
- npm i sequelize -g | ||
- sequelize db:migrate | ||
after_success: | ||
- npm test | ||
- npm run coveralls |
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,60 @@ | ||
import db from '../models'; | ||
import Auth from '../helpers/auth'; | ||
|
||
export const signup = async (req, res) => { | ||
const { | ||
userName, firstName, lastName, email | ||
} = req.body; | ||
const password = Auth.hashPassword(req.body.password); | ||
const transaction = await db.sequelize.transaction(); | ||
try { | ||
const newUser = await db.User.create({ | ||
userName, firstName, lastName, email, password | ||
}, { | ||
transaction | ||
}); | ||
await transaction.commit(); | ||
if (newUser) { | ||
res.status(201).json({ | ||
success: 1, | ||
token: Auth.genToken(userName, firstName, lastName, email), | ||
user: { | ||
username: newUser.userName, | ||
email: newUser.email | ||
}, | ||
message: 'created successfully' | ||
}); | ||
} | ||
} catch (ex) { | ||
await transaction.rollback(); | ||
res.json({ success: 0, message: `${ex.errors[0].path.toLowerCase()} already exists` }); | ||
} | ||
}; | ||
|
||
export const signin = async (req, res) => { | ||
const { email, password } = req.body; | ||
const transaction = await db.sequelize.transaction(); | ||
try { | ||
const user = await db.User.findOne({ where: { email } }, { transaction }); | ||
await transaction.commit(); | ||
if (!user) { | ||
return res.status(401).json({ success: 0, message: 'user doesnot exit' }); | ||
} | ||
const passBool = Auth.comparePassword(password, user.password); | ||
if (passBool) { | ||
return res.status(200).json({ | ||
success: 1, | ||
token: Auth.genToken(user.userName, user.firstName, user.lastName, user.email), | ||
user: { | ||
username: user.userName, | ||
email: user.email | ||
}, | ||
message: 'logged in' | ||
}); | ||
} | ||
return res.status(401).json({ success: 0, message: 'wrong username or password' }); | ||
} catch (ex) { | ||
await transaction.rollback(); | ||
res.json({ success: 0, message: ex }); | ||
} | ||
}; |
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 @@ | ||
export * from './auth'; | ||
export * from './welcomeController'; |
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,13 +1,9 @@ | ||
import dotenv from "dotenv"; | ||
import db from "../models/databaseUrl"; | ||
dotenv.config(); | ||
import dotenv from 'dotenv'; | ||
|
||
class WelcomeController { | ||
static welcome(req, res) { | ||
res.status(200).json({ | ||
msg: "Welcome to Authors Haven API, a #92Explorers Product." | ||
}); | ||
} | ||
} | ||
dotenv.config(); | ||
|
||
export default WelcomeController; | ||
export const welcome = (req, res) => { | ||
res.status(200).json({ | ||
msg: 'Welcome to Authors Haven API, a #92Explorers Product.' | ||
}); | ||
}; |
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,27 @@ | ||
import bcrypt from 'bcrypt'; | ||
import jwt from 'jsonwebtoken'; | ||
import dontenv from 'dotenv'; | ||
|
||
dontenv.config(); | ||
|
||
class Auth { | ||
static hashPassword(password) { | ||
return bcrypt.hashSync(password, bcrypt.genSaltSync(10)); | ||
} | ||
|
||
static comparePassword(password, hashedPassword) { | ||
return bcrypt.compareSync(password, hashedPassword); | ||
} | ||
|
||
static genToken(username, firstname, lastname, email) { | ||
const token = jwt.sign({ | ||
email, | ||
username, | ||
firstname, | ||
lastname, | ||
}, process.env.SECRET, { expiresIn: '24h' }); | ||
return token; | ||
} | ||
} | ||
|
||
export default Auth; |
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,35 @@ | ||
import jwt from 'jsonwebtoken'; | ||
import dotenv from 'dotenv'; | ||
|
||
dotenv.config(); | ||
|
||
export const checkToken = (req, res, next) => { | ||
let token = req.headers.authorization || req.headers['x-access-token']; | ||
if (token === undefined) { | ||
return res.json({ | ||
message: 'token undefined', | ||
}); | ||
} | ||
|
||
if (token.startsWith('Bearer ')) { | ||
token = token.slice(7, token.length); | ||
} | ||
|
||
if (token) { | ||
jwt.verify(token, process.env.SECRET, (error, decoded) => { | ||
if (error) { | ||
return res.status(401).json({ | ||
success: false, | ||
message: error.message, | ||
}); | ||
} | ||
req.decoded = decoded; | ||
next(); | ||
}); | ||
} else { | ||
return res.status(401).json({ | ||
success: false, | ||
message: 'token empty', | ||
}); | ||
} | ||
}; |
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 @@ | ||
export * from './checkToken'; |
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,39 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('Users', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
firstName: { | ||
type: Sequelize.STRING | ||
}, | ||
lastName: { | ||
type: Sequelize.STRING | ||
}, | ||
email: { | ||
type: Sequelize.STRING, | ||
unique: true | ||
}, | ||
userName: { | ||
type: Sequelize.STRING, | ||
unique: true, | ||
}, | ||
provider: { | ||
type: Sequelize.STRING | ||
}, | ||
password: { | ||
type: Sequelize.STRING | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}), | ||
down: queryInterface => queryInterface.dropTable('Users') | ||
}; |
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,11 @@ | ||
export default (sequelize, DataTypes) => { | ||
const User = sequelize.define('User', { | ||
firstName: DataTypes.STRING, | ||
lastName: DataTypes.STRING, | ||
email: DataTypes.STRING, | ||
userName: DataTypes.STRING, | ||
provider: DataTypes.STRING, | ||
password: DataTypes.STRING | ||
}, {}); | ||
return User; | ||
}; |
This file was deleted.
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
Oops, something went wrong.