Created
May 19, 2020 11:07
-
-
Save Parables/5c8d5ac293ca3d0b0d4dc830fa88429e to your computer and use it in GitHub Desktop.
hapi cookie authentication
This file contains hidden or 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
| const Hapi = require('@hapi/hapi'); | |
| const bcrypt = require('bcrypt'); | |
| import { User, UserType } from './model'; | |
| import connectDB, { user, createUser, updateUser } from './controller' | |
| const init = async () => { | |
| var server = Hapi.server({ | |
| port: process.env.PORT || 3000, | |
| host: process.env.HOST || '0.0.0.0' || 'localhost', | |
| routes: { | |
| "cors": true | |
| } | |
| }); | |
| await server.register(require('@hapi/cookie')); | |
| server.auth.strategy('session', 'cookie', { | |
| cookie: { | |
| name: 'calvary', | |
| password: 'somepassword', | |
| isSecure: false, | |
| ttl: 24 * 60 * 60 * 1000 | |
| }, | |
| redirectTo: '/signin', | |
| validateFunc: async (request, session) => { | |
| console.log("Seession", session) | |
| const account = await user(session.username) | |
| if (!account) { | |
| return { valid: false }; | |
| } | |
| return { valid: true, credentials: account }; | |
| } | |
| }); | |
| server.auth.default('session'); | |
| server.route([ | |
| { | |
| method: 'GET', | |
| path: '/', | |
| handler: (request, h) => { | |
| return 'Connected to server'; | |
| } | |
| }, { | |
| method: ['POST', 'PATCH'], | |
| path: '/signup', | |
| handler: (request, h) => { | |
| const payload: User = typeof request.payload === 'string' ? JSON.parse(request.payload) : request.payload | |
| console.log("PAYLOAD", payload, typeof payload) // logs undefined | |
| let { error, value } = UserType.validate(payload); | |
| console.log("Logging JOI results: ", value, error) | |
| if (error) return error | |
| bcrypt.hash(value.password, 10).then(async function (hash) { | |
| value.password = hash | |
| console.log("Hashing", value.password, hash) | |
| const result = request.method === 'post' ? await createUser(value) : await updateUser(value, value.id) | |
| return result ? result.toObject() : "No data returned: Error 039"; | |
| }); | |
| } | |
| }, | |
| { | |
| method: 'GET', | |
| path: '/signin', | |
| handler: function (request, h) { | |
| //return h.response('unauthorized').code(401) | |
| return `inside here is the login.html code`; | |
| }, | |
| options: { | |
| auth: false | |
| } | |
| }, | |
| { | |
| method: 'POST', | |
| path: '/signin', | |
| handler: async (request, h) => { | |
| console.log(JSON.stringify(request.payload)) | |
| const u: User = typeof request.payload === 'string' ? JSON.parse(request.payload) : request.payload; | |
| console.log("Logging creds", typeof request.payload, u, u.username, u.password,) | |
| const account = await user(u.username) | |
| console.log("Account", account) | |
| if (!account || !(await bcrypt.compare(u.password, account.password))) { | |
| return h.view('/signin'); | |
| } | |
| request.cookieAuth.set({ username: account.username }); | |
| return h.redirect('/'); | |
| }, | |
| options: { | |
| auth: { | |
| mode: 'try' | |
| } | |
| } | |
| } | |
| ]); | |
| await server.start(); | |
| console.log('🌎 Server running on %s', server.info.uri); | |
| }; | |
| process.on('unhandledRejection', (err) => { | |
| console.log(err); | |
| process.exit(1); | |
| }); | |
| connectDB().then(() => init()) |
This file contains hidden or 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
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>Login</title> | |
| <style> | |
| ... my styles here ... | |
| </style> | |
| </head> | |
| <body> | |
| <div class="divone"> | |
| <div class="divtwo"> | |
| <h1>Welcome, please sign into your account</h1> | |
| <form method="post" action="/signin"> | |
| <input type="text" id="username" autocomplete="username" placeholder="johndoe@gmail.com" | |
| class="mt-4 txtInput"> | |
| <input type="password" id="password" placeholder="password" autocomplete="current-password" | |
| class="my-4 txtInput"> | |
| <input type="submit" class="btn" value="Sign In"> | |
| </form> | |
| <p>©2020 ParaSoft Dev Studio. All rights reserved.</p> | |
| </div> | |
| </div> | |
| <!-- <script> | |
| let data = JSON.stringify({ | |
| username: "John Doe", | |
| password: "secret" | |
| }) | |
| var username = document.getElementById("username").value; | |
| var password = document.getElementById("password").value; | |
| console.log(data, username, password) | |
| formElem.onsubmit = async (e) => { | |
| let response = await fetch('/signin', { | |
| method: 'POST', | |
| body: data | |
| }); | |
| console.log(response) | |
| }; | |
| </script> --> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment