Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
RoleGame
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Martin Lehoux
RoleGame
Commits
8422e15c
Commit
8422e15c
authored
6 years ago
by
Kagamino
Browse files
Options
Downloads
Patches
Plain Diff
authentication works
parent
60f25c59
No related branches found
No related tags found
2 merge requests
!2
Release 0.2: Notifications & Errors
,
!1
Release 0.1: Authentication & Session
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
index.js
+39
-20
39 additions, 20 deletions
index.js
package.json
+1
-0
1 addition, 0 deletions
package.json
views/base.pug
+21
-0
21 additions, 0 deletions
views/base.pug
with
61 additions
and
20 deletions
index.js
+
39
−
20
View file @
8422e15c
...
...
@@ -4,12 +4,21 @@ const bodyParser = require('body-parser');
const
User
=
require
(
'
./models/user
'
);
const
mongoose
=
require
(
'
mongoose
'
);
const
bcrypt
=
require
(
'
bcrypt
'
);
const
morgan
=
require
(
'
morgan
'
);
const
config
=
require
(
'
./config.json
'
);
// Utils
const
render
=
(
req
,
res
,
view
,
options
)
=>
res
.
render
(
view
,
{
...
options
,
user
:
req
.
session
.
user
,
nextUrl
:
req
.
url
,
});
// Configuration
const
app
=
express
();
app
.
set
(
'
view engine
'
,
'
pug
'
);
app
.
use
(
morgan
(
'
tiny
'
));
app
.
use
(
session
({
secret
:
config
.
secret
,
resave
:
false
,
...
...
@@ -21,42 +30,52 @@ app.use(bodyParser.urlencoded({
extended
:
false
,
}));
app
.
use
((
req
,
res
,
next
)
=>
{
if
(
!
req
.
session
.
user
&&
!
[
'
/
'
,
'
/signup
'
].
includes
(
req
.
url
))
{
return
res
.
redirect
(
'
/signup
'
);
if
(
req
.
session
.
user
||
[
'
/
'
,
'
/signup
'
,
'
/login
'
].
includes
(
req
.
url
))
{
next
(
);
}
else
{
return
next
(
);
return
res
.
redirect
(
'
/signup
'
);
}
});
app
.
get
(
'
/
'
,
(
req
,
res
)
=>
{
return
res
.
render
(
'
home
'
);
return
render
(
req
,
res
,
'
home
'
);
});
app
.
get
(
'
/signup
'
,
(
req
,
res
)
=>
{
return
res
.
render
(
'
signup
'
);
return
render
(
req
,
res
,
'
signup
'
);
});
app
.
post
(
'
/signup
'
,
(
req
,
res
)
=>
{
bcrypt
.
hash
(
req
.
body
.
password
,
config
.
cryptRounds
).
then
((
hash
,
err
)
=>
{
User
.
create
(
req
.
body
).
then
(
user
=>
{
const
passwordHash
=
bcrypt
.
hashSync
(
req
.
body
.
password
,
config
.
cryptRounds
);
User
.
create
(
req
.
body
,
(
err
,
user
)
=>
{
err
?
console
.
error
(
err
)
:
null
;
user
.
passwordHash
=
passwordHash
;
user
.
save
();
req
.
session
.
user
=
user
;
return
res
.
redirect
(
'
/
'
);
});
});
});
app
.
post
(
'
/login
'
,
(
req
,
res
)
=>
{
User
.
findOne
({
username
:
req
.
body
.
username
}
).
then
(
user
=>
{
bcrypt
.
compare
(
req
.
body
.
password
,
user
.
passwordHash
).
then
((
err
,
same
)
=>
{
if
(
same
)
{
User
.
findOne
({
username
:
req
.
body
.
username
}
,
(
err
,
user
)
=>
{
err
?
console
.
error
(
err
)
:
null
;
if
(
bcrypt
.
compareSync
(
req
.
body
.
password
,
user
.
passwordHash
)
)
{
req
.
session
.
user
=
user
;
return
res
.
redirect
(
req
.
query
.
nextUrl
);
return
res
.
redirect
(
req
.
query
.
nextUrl
||
'
/
'
);
}
else
{
console
.
error
(
"
Bad authentication
"
);
return
res
.
redirect
(
'
/signup
'
);
}
});
});
});
app
.
post
(
'
/logout
'
,
(
req
,
res
)
=>
{
req
.
session
.
destroy
();
return
res
.
redirect
(
'
/
'
);
})
mongoose
.
connect
(
'
mongodb://localhost/rolegame
'
,
err
=>
{
if
(
err
)
{
console
.
error
(
'
ERROR Unable to connect to Mongo database
'
)
}
else
{
console
.
log
(
'
Server connected to Mongo database
'
);
}
app
.
listen
(
config
.
port
,
()
=>
{
console
.
log
(
`Server listening on http://localhost:
${
config
.
port
}
`
);
});
...
...
This diff is collapsed.
Click to expand it.
package.json
+
1
−
0
View file @
8422e15c
...
...
@@ -9,6 +9,7 @@
"
express
"
:
"
^4.16.4
"
,
"
express-session
"
:
"
^1.15.6
"
,
"
mongoose
"
:
"
^5.4.11
"
,
"
morgan
"
:
"
^1.9.1
"
,
"
nodemon
"
:
"
^1.18.10
"
,
"
pug
"
:
"
^2.0.3
"
}
...
...
This diff is collapsed.
Click to expand it.
views/base.pug
+
21
−
0
View file @
8422e15c
...
...
@@ -8,6 +8,27 @@ html(lang="en")
title RoleGame
body
block navbar
if !user
form.ui.form(action="/login", method="post")
.ui.pointing.menu
.right.menu
.item
.ui.input.transparent
input#username(type="text" name="username" placeholder="username")
.item
.ui.transparent.input
input#password(type="password" name="password" placeholder="password")
.item
.ui.transparent.input
input.ui.button(type="submit" value="Se connecter")
else
form.ui.form(action="/logout", method="post")
.ui.pointing.menu
.right.menu
.item #{user.firstName} #{user.lastName}
.item
button.ui.icon.button.basic(type="submit")
i.power.off.icon
.ui.container
block main
block script
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment