Answer the question
In order to leave comments, you need to log in
How to work with express-layout?
I am signing in/registering with NodeJS.
How to make the transition from the login to registration form or vice versa change the forms.
I watched video tutorials, articles where they show how to work with express-layout.
There is such html markup (master page):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel = "stylesheet" type = "text/css" href = "../views/styles/entrance.css"/>
<link rel = "stylesheet" type = "text/css" href = "../styles/login-forms.css"/>
<link href="https://fonts.googleapis.com/css?family=Big+Shoulders+Text&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Exo+2&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Poppins&display=swap" rel="stylesheet">
<title>BackEnd NodeJS Attempts</title>
</head>
<body>
<main>
<ul>
<li>Login</li>
<li>Register</li>
</ul>
</main>
<aside>
<%- body -%>
</aside>
</body>
</html>
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}
const express = require('express')
const ejsLocals = require('ejs-locals')
const expressLayouts = require('express-ejs-layouts')
const app = express()
const bcrypt = require('bcrypt')
const passport = require('passport')
const flash = require('express-flash')
const session = require('express-session')
const methodOverride = require('method-override')
const connection = require('./models/database');
app.use(express.static('views'))
const initializePassport = require('./models/passport-config')
initializePassport(
passport,
email => users.find(user => user.email === email),
id => users.find(user => user.id === id)
)
const users = []
app.set('view-engine', 'ejs')
app.engine('ejs', ejsLocals)
app.set(expressLayouts)
app.use(express.urlencoded({ extended: false }))
app.use(flash())
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false
}))
app.use(passport.initialize())
app.use(passport.session())
app.use(methodOverride('_method'))
app.get('/', checkAuthenticated, (req, res) => {
res.render('index.ejs', { name: req.user.name })
})
app.get('/login', checkNotAuthenticated, (req, res) => {
res.render('login.ejs')
})
app.post('/login', checkNotAuthenticated, passport.authenticate('local', {
successRedirect: '/',
failureRedirect: '/login',
failureFlash: true
}))
app.get('/register', checkNotAuthenticated, (req, res) => {
res.render('register.ejs')
})
app.post('/register', checkNotAuthenticated, async (req, res) => {
try {
const hashedPassword = await bcrypt.hash(req.body.password, 10)
users.push({
id: Date.now().toString(),
name: req.body.name,
email: req.body.email,
password: hashedPassword
})
res.redirect('/login')
} catch {
res.redirect('/register')
}
})
app.delete('/logout', (req, res) => {
req.logOut()
res.redirect('/login')
})
function checkAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return next()
}
res.redirect('/login')
}
function checkNotAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return res.redirect('/')
}
next()
}
app.listen(3000)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question