From e5991136faf80e2b321a48cc7f1c760e7688779b Mon Sep 17 00:00:00 2001 From: Hajar RAHMOUNI Date: Wed, 4 Oct 2023 22:56:20 +0200 Subject: [PATCH] role user added (agent or consultant) --- routes/authentification.js | 46 +++++++++++++++++--------------------- schemas/models.js | 3 ++- views/register.pug | 8 ++++++- 3 files changed, 29 insertions(+), 28 deletions(-) diff --git a/routes/authentification.js b/routes/authentification.js index 8ceb94b..77c551a 100644 --- a/routes/authentification.js +++ b/routes/authentification.js @@ -22,37 +22,31 @@ router.get('/register', function(req, res) { }); router.post('/register', async function(req, res) { - /*User.register(new User({ username : req.body.username }), req.body.password, function(err, account) { - if (err) { - return res.render('register', { account : account }); - } - - passport.authenticate('local')(req, res, function () { - res.redirect('/'); - }); - });"*/ - try { - const { username, password } = req.body; - const existingUser = await User.findOne({ username }); + try { + const { username, password, role } = req.body; + const existingUser = await User.findOne({ username }); - if (existingUser) { - return res.render('register', { error: 'Cet utilisateur existe déjà.' }); - } + if (existingUser) { + return res.render('register', { error: 'Cet utilisateur existe déjà.' }); + } - const newUser = new User({ - username: username, - password: password - }); - await newUser.save(); + const newUser = new User({ + username: username, + password: password, + isAgent: role === 'agent' + }); - passport.authenticate('local')(req, res, function () { - res.redirect('/'); - }); - } catch (error) { - res.render('register', { error: error.message }); - } + await newUser.save(); + + passport.authenticate('local')(req, res, function () { + res.redirect('/'); + }); + } catch (error) { + res.render('register', { error: error.message }); + } }); + router.get('/logout', isAuthenticated, function(req, res, next) { req.logout(function(err) { if (err) { diff --git a/schemas/models.js b/schemas/models.js index d961e58..8d4de7f 100644 --- a/schemas/models.js +++ b/schemas/models.js @@ -4,7 +4,8 @@ var passportLocalMongoose = require('passport-local-mongoose'); var userSchema = new Schema({ username: { type: String, required: true }, - password: { type: String, required: true } + password: { type: String, required: true }, + isAgent: { type: Boolean, default: false } }); userSchema.plugin(passportLocalMongoose); diff --git a/views/register.pug b/views/register.pug index 3d1cc36..c7cd895 100644 --- a/views/register.pug +++ b/views/register.pug @@ -5,11 +5,17 @@ block content h1 Register p.lead Créez votre compte br - form(role='form', action="/register",method="post", style='max-width: 300px;') + form(role='form', action="/register", method="post", style='max-width: 300px;') .form-group input.form-control(type='text', name="username", placeholder='Enter Username') .form-group input.form-control(type='password', name="password", placeholder='Password') + + .form-group + select.form-control(name='role') + option(value='consultant') Consultant + option(value='agent') Agent + button.btn.btn-default(type='submit') Submit a(href='/') button.btn.btn-primary(type="button") Cancel -- GitLab