Environment Config
This commit is contained in:
22
server-node/src/auth/auth-controller.ts.ts
Normal file
22
server-node/src/auth/auth-controller.ts.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import passport from "passport";
|
||||
import session from "express-session";
|
||||
import { initialize } from "./passport-config";
|
||||
import auth from "../routes/auth/auth-controller";
|
||||
import { Express } from "express";
|
||||
|
||||
export function connect(app: Express) {
|
||||
app.use(session({
|
||||
secret: process.env.SESSION_SECRET || "default-secret",
|
||||
resave: false,
|
||||
saveUninitialized: false
|
||||
}));
|
||||
|
||||
app.use(passport.initialize());
|
||||
app.use(passport.authenticate(['headerapikey', 'session'], {
|
||||
session: false, // Only set a session on the login request.
|
||||
}));
|
||||
|
||||
initialize(passport);
|
||||
|
||||
app.use("/auth", auth);
|
||||
}
|
||||
@@ -38,7 +38,7 @@ export function initialize(passport: typeof import("passport")) {
|
||||
));
|
||||
|
||||
passport.serializeUser((user, done) => {
|
||||
done(null, user.id) //TODO: Extend Express.User to include id wich is set by passport
|
||||
done(null, user.id)
|
||||
});
|
||||
|
||||
passport.deserializeUser((id: number, done) => {
|
||||
|
||||
@@ -33,13 +33,13 @@ export async function verifyPassword(user: User, password: string): Promise<bool
|
||||
if(!passwordRecord) {
|
||||
throw new Error("This user does not have a password set!");
|
||||
}
|
||||
return passwordRecord.password == password;
|
||||
return passwordRecord.password == password; // TODO: Replace with web-crypto
|
||||
}
|
||||
|
||||
export function createUser(params: { username: string, password: string }, cb: (err: SequelizeError | null, user: User | null) => void ) {
|
||||
User.create({ username: params.username, authenticationMethod: "password" }).then(async user => {
|
||||
user.setPassword(await Password.create({
|
||||
password: params.password,
|
||||
password: params.password, // TODO: Replace with web-crypto
|
||||
})).then(password => {
|
||||
cb(null, user as any as User)
|
||||
}).catch(e =>
|
||||
|
||||
Reference in New Issue
Block a user