Last active
May 16, 2017 02:02
-
-
Save JoelCodes/c7e659d5c59236e75ceada06758162a4 to your computer and use it in GitHub Desktop.
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"> | |
| <meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
| <title>Document</title> | |
| </head> | |
| <body> | |
| <%= JSON.stringify(user) %> | |
| </body> | |
| </html> |
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"> | |
| <meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
| <title>Document</title> | |
| </head> | |
| <body> | |
| <form action="/login" method='post'> | |
| <input type='email' name='email' placeholder='email'/> | |
| <input type='password' name='password' placeholder='password'/> | |
| <input type='submit' value='Log in'/> | |
| </form> | |
| </body> | |
| </html> |
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
| { | |
| "name": "w3d1", | |
| "version": "1.0.0", | |
| "description": "", | |
| "main": "index.js", | |
| "scripts": { | |
| "test": "echo \"Error: no test specified\" && exit 1" | |
| }, | |
| "keywords": [], | |
| "author": "", | |
| "license": "ISC", | |
| "dependencies": { | |
| "body-parser": "^1.17.1", | |
| "cookie-parser": "^1.4.3", | |
| "ejs": "^2.5.6", | |
| "express": "^4.15.2" | |
| } | |
| } |
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
| var express = require('express'); | |
| var cookieParser = require('cookie-parser'); | |
| var bodyParser = require('body-parser'); | |
| const userDB = [{id: '1', email: 'asdf@asdf', password: 'asdf'}]; | |
| const urlDB = { | |
| 'A1234Z': {longUrl: 'www.google.ca', userId: '1', shortUrl: 'A1234Z'}, | |
| 'B1234Y': {longUrl: 'www.pinterest.com', userId: '2', shortUrl: 'B1234Y'}, | |
| 'C1234X': {longUrl: 'www.pinterest.com', userId: '1', shortUrl: 'C1234X'}}; | |
| const urlRouter = require('./urls-router')(urlDB, userDB); | |
| var app = express(); | |
| app.use(cookieParser()); | |
| app.use(bodyParser.urlencoded({ | |
| extended: true | |
| })); | |
| app.set('view engine', 'ejs'); | |
| app.use((req, res, next) => { | |
| req.user = userDB.find(user => user.id === req.cookies.user_id); | |
| res.locals.user = req.user; | |
| next(); | |
| }); | |
| app.use('/urls', urlRouter); | |
| app.get('/', (req, res) => { | |
| console.log(req.user); | |
| res.render('index'); | |
| }); | |
| app.get('/login', (req, res) => { | |
| res.render('login'); | |
| }); | |
| app.post('/login', (req, res) => { | |
| const {email, password} = req.body; | |
| const foundUser = userDB.find(user => user.email === email && user.password === password); | |
| if(foundUser === undefined){ | |
| res.redirect('/login'); | |
| } else { | |
| res.cookie('user_id', foundUser.id); | |
| res.redirect('/'); | |
| } | |
| }); | |
| app.listen(3000); |
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
| function makeRouter(urlsDB, userDB){ | |
| const router = new require('express').Router(); | |
| router.use((req, res, next) => { | |
| if(req.user === undefined){ | |
| res.redirect('/login'); | |
| } else { | |
| next(); | |
| } | |
| }); | |
| router.get('/', (req, res) => { | |
| res.send('Urls Page'); | |
| }) | |
| router.get('/:id', (req, res) => { | |
| res.send(`Page for ${req.params.id}`); | |
| }); | |
| return router; | |
| } | |
| module.exports = makeRouter; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment