Skip to content
This repository was archived by the owner on May 28, 2024. It is now read-only.

Commit 443a60f

Browse files
committed
fix some bugs
1 parent c228cbd commit 443a60f

12 files changed

+30
-50
lines changed

.env.sample

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
PORT=8000
2+
GITHUB_CLIENT_ID=1234567890
3+
GITHUB_CLIENT_SECRET=abcdefghijklmnopqrstuvwxyz

middleware/PassportConfig.ts

+4-11
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,11 @@ import passport from 'passport'
33
import { PassportStrategy } from '../interfaces'
44

55
export default class PassportConfig {
6-
/*
7-
FIX ME 😭
8-
The problem with this class is... if the caller forgets to call
9-
the addStrategies method...our program won't work.
6+
constructor(strategies: PassportStrategy[]) {
7+
this.#addStrategies(strategies)
8+
}
109

11-
Solution: You should refactor this class to take a constructor
12-
which receives strategies: PassportStrategy[]. Internally...call
13-
the addStrategies method within the constructor and make addStragies
14-
private from the outside world. This way, we can GUARANTEE that our
15-
passport strategies are added when this class is created. ⭐️
16-
*/
17-
addStrategies(strategies: PassportStrategy[]): void {
10+
#addStrategies(strategies: PassportStrategy[]): void {
1811
strategies.forEach((passportStrategy: PassportStrategy) => {
1912
passport.use(passportStrategy.name, passportStrategy.strategy)
2013
})

middleware/checkAuth.ts

-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
import { NextFunction, Request, Response } from 'express'
22

3-
/*
4-
FIX ME (types) 😭
5-
*/
63
export const ensureAuthenticated = (req: Request, res: Response, next: NextFunction) => {
74
if (req.isAuthenticated()) {
85
return next()
96
}
107
res.redirect('/auth/login')
118
}
129

13-
/*
14-
FIX ME (types) 😭
15-
*/
1610
export const forwardAuthenticated = (req: Request, res: Response, next: NextFunction) => {
1711
if (!req.isAuthenticated()) {
1812
return next()

middleware/passportMiddleware.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import localStrategy from './passportStrategies/localStrategy'
66
import passportGitHubStrategy from './passportStrategies/githubStrategy'
77

88
// No need to actually pass the instance of passport since it returns a singleton
9-
const passportConfig = new PassportConfig()
10-
passportConfig.addStrategies([localStrategy, passportGitHubStrategy])
9+
new PassportConfig([localStrategy, passportGitHubStrategy])
1110
const passportMiddleware = (app: Application): void => {
1211
app.use(passport.initialize())
1312
app.use(passport.session())

middleware/passportStrategies/githubStrategy.ts

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const githubStrategy: GitHubStrategy = new GitHubStrategy(
1414
passReqToCallback: true,
1515
},
1616

17-
/* FIX ME 😭 */
1817
async (
1918
req: Request,
2019
accessToken: string,

middleware/passportStrategies/localStrategy.ts

-4
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ const localStrategy = new LocalStrategy(
2424
}
2525
)
2626

27-
/*
28-
FIX ME (types) 😭
29-
*/
30-
3127
passport.serializeUser(function (user, done) {
3228
done(null, user.id)
3329
})

models/userModel.ts

-2
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,13 @@ const database: Express.User[] = [
2323
]
2424

2525
const userModel = {
26-
/* FIX ME (types) 😭 */
2726
findOne: (email: string) => {
2827
const user = database.find((user) => user.email === email)
2928
if (user) {
3029
return user
3130
}
3231
throw new Error(`Couldn't find user with email: ${email}`)
3332
},
34-
/* FIX ME (types) 😭 */
3533
findById: (id: number) => {
3634
const user = database.find((user) => user.id === id)
3735
if (user) {

routes/authRoute.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ declare module 'express-session' {
1212

1313
router.get('/login', forwardAuthenticated, (req, res) => {
1414
res.render('login', {
15-
messages: req.session.messages || [],
15+
message: req.session.messages?.pop(),
1616
})
1717
})
1818

@@ -21,7 +21,6 @@ router.post(
2121
passport.authenticate('local', {
2222
successRedirect: '/dashboard',
2323
failureRedirect: '/auth/login',
24-
/* FIX ME: 😭 failureMsg needed when login fails */
2524
failureMessage: true,
2625
})
2726
)

routes/indexRoute.ts

+10-16
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,20 @@ router.get('/', (req, res) => {
88
})
99

1010
router.get('/dashboard', ensureAuthenticated, (req, res) => {
11-
if (req.user?.role === 'admin') {
12-
res.redirect('/admin')
13-
} else {
14-
res.render('dashboard', {
15-
user: req.user,
16-
})
17-
}
11+
res.render('dashboard', {
12+
user: req.user,
13+
})
1814
})
1915

2016
router.get('/admin', ensureAuthenticated, (req, res) => {
21-
if (req.user?.role === 'admin') {
22-
const store = req.sessionStore
23-
store.all &&
24-
store.all((err, sessions) => {
25-
console.log(sessions)
26-
res.render('admin', {
27-
user: req.user,
28-
sessions,
29-
})
17+
const store = req.sessionStore
18+
if (req.user?.role === 'admin' && store.all) {
19+
store.all((err, sessions) => {
20+
res.render('admin', {
21+
user: req.user,
22+
sessions,
3023
})
24+
})
3125
} else {
3226
res.redirect('/dashboard')
3327
}

views/admin.ejs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
<h1 class="mt-4">Dashboard</h1>
1+
<h1 class="mt-4">Administration</h1>
22
<p class="lead mb-3">Welcome <%= user.name %>
33
</p>
44
<p>Current Active Sessions:</p>
55

66
<% for (const key in sessions) { %>
7-
<div>
8-
<div>SessionId: <%=key%></div>
9-
<div>UserId: <%=sessions[key].passport.user%></div>
10-
<div><a href="/admin/revoke/<%=key%>">Revoke</a></div>
7+
<div style="margin-top: 24px;">
8+
<div>SessionID: <%=key%></div>
9+
<div>UserID: <%=sessions[key].passport.user%></div>
10+
<div><a href="/admin/revoke/<%=key%>">Revoke Session</a></div>
1111
</div>
1212
<% } %>

views/dashboard.ejs

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
<h1 class="mt-4">Dashboard</h1>
22
<p class="lead mb-3">Welcome <%= user.name %></p>
3+
<% if (user.role === 'admin') { %>
4+
<p>
5+
<a href="/admin">Sessions Administration</a>
6+
</p>
7+
<% } %>
38
<a href="/auth/logout" class="btn btn-secondary">Logout</a>

views/login.ejs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<img src="/secure-icon.png" alt="icon" width="20%" />
66
</h1>
77
<p style="color: #c00">
8-
<%= messages[messages.length - 1] %>
8+
<%= message %>
99
</p>
1010

1111
<h2>Login</h2>

0 commit comments

Comments
 (0)