Skip to content

Commit

Permalink
User can now login
Browse files Browse the repository at this point in the history
  • Loading branch information
StanleyMasinde committed May 2, 2021
1 parent 11e3cc2 commit b9fe06b
Show file tree
Hide file tree
Showing 10 changed files with 480 additions and 13 deletions.
17 changes: 17 additions & 0 deletions backend/app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const express = require('express')
const session = require('express-session')
const sessionstore = require('sessionstore')
const cookieParser = require('cookie-parser')
const logger = require('morgan')

Expand All @@ -8,6 +10,21 @@ const authRouter = require('./routes/auth')

const app = express()

app.use(
session({
secret: 'super-secret-cookie', // TODO add this to .env
resave: false,
saveUninitialized: true,
name: 'deployer_session',
store:
process.env.NODE_ENV === 'testing'
? null
: sessionstore.createSessionStore({
type: 'redis',
}),
})
)

app.use(logger('dev'))
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
Expand Down
58 changes: 58 additions & 0 deletions backend/app/controllers/authController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const { compareSync } = require('bcrypt')
const User = require('../models/user')
const Controller = require('./controller')

class AuthController extends Controller {
/**
* Attempt to authenticate a user with email and pass
* @param {*} request
*/
async attempt(req, res, next) {
const failedResponse = {
errors: { email: ['These credentials do not match our records'] },
}
const { email, password } = req.body
try {
const user = await User.where({ email }).first()
if (!user) {
res.status(422).json(failedResponse) // The mail does not match
}
const passwordMatches = compareSync(password, user.password)
if (passwordMatches) {
delete user.password
req.session.userId = user.id
return res.json(user) // Login successful
}
res.status(422).json(failedResponse) // login failed
} catch (error) {
res.status(500).json(error) // A server error occoured
}
}

/**
* Get the current authenticated user
* @param {*} req
* @param {*} res
*/
async getUser(req, res) {
const failedResponse = {
message: 'You are not authenticated',
}
const { userId } = req.session
if (!userId) {
res.status(401).json(failedResponse)
}
try {
const user = await User.find(userId)
if (!user) {
res.status(401).json(failedResponse)
}
delete user.password
res.json({ user })
} catch (error) {
res.status(500).json(error)
}
}
}

module.exports = new AuthController()
3 changes: 3 additions & 0 deletions backend/app/controllers/controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Controller {}

module.exports = Controller
5 changes: 3 additions & 2 deletions backend/routes/auth.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
const router = require('express').Router()
const authController = require('../app/controllers/authController')

/**
*
* Authenticate a user using email and password
*/
router.post('/login', (req, res) => {
res.json('Login route')
authController.attempt(req, res)
})

/**
* Get the current authenticated user
*
*/
router.get('/user', (req, res) => {
res.json('The current user')
authController.getUser(req, res)
})

/**
Expand Down
26 changes: 24 additions & 2 deletions nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export default {

// Global page headers: https://go.nuxtjs.dev/config-head
head: {
titleTemplate: '%s - deployer',
title: 'deployer',
titleTemplate: null,
title: 'Deployer',
meta: [
{ charset: 'utf-8' },
{ name: 'viewport', content: 'width=device-width, initial-scale=1' },
Expand Down Expand Up @@ -91,6 +91,28 @@ export default {
},
},

/**
* Auth configuration
*/
auth: {
strategies: {
local: {
user: {
property: 'user',
},
token: {
required: false,
type: false,
},
endpoints: {
login: { url: '/api/auth/login', method: 'POST' },
logout: { url: '/api/auth/logout', method: 'POST' },
user: { url: '/api/auth/user', method: 'GET' },
},
},
},
},

// Build Configuration: https://go.nuxtjs.dev/config-build
build: {
babel: {
Expand Down
Loading

0 comments on commit b9fe06b

Please sign in to comment.