-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassport.js
More file actions
46 lines (40 loc) · 1.16 KB
/
passport.js
File metadata and controls
46 lines (40 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import passport from "passport";
import { Strategy as LocalStrategy } from "passport-local";
import { User } from "./models/sql/user";
import lodash from "lodash";
const UNAUTH_URLS = ["/member/login"];
export const initializePassport = (app) => {
app.use(passport.initialize());
app.use(passport.session());
app.use(function (req, res, next) {
if (UNAUTH_URLS.indexOf(req.path) > -1) {
return next();
} else if (req.isAuthenticated()) {
return next();
}
res.status(401).send({ error: "unauthorized" }).end();
});
passport.use(
new LocalStrategy(function (username, password, done) {
User.findOne({ where: { username: username, password: password } })
.then((user) => {
if (!user) {
return done(null, false);
}
return done(
null,
lodash.pick(user, ["uuid", "username", "firstname", "lastname"])
);
})
.catch((err) => {
console.log(err);
});
})
);
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(function (user, done) {
done(null, user);
});
};