-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix session and update home page projects
- Loading branch information
Showing
29 changed files
with
392 additions
and
268 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"jsx": "react", | ||
"module": "NodeNext", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
|
||
import { prisma } from "@celluloid/prisma" | ||
import bcrypt from 'bcryptjs'; | ||
import passport from 'passport'; | ||
import { | ||
Strategy as LocalStrategy, | ||
} from "passport-local"; | ||
|
||
export enum SigninStrategy { | ||
LOGIN = "login", | ||
TEACHER_SIGNUP = "teacher-signup", | ||
STUDENT_SIGNUP = "student-signup", | ||
} | ||
|
||
|
||
passport.serializeUser((user: any, done) => { | ||
done(null, user.id) | ||
}); | ||
|
||
passport.deserializeUser(async (id: string, done) => { | ||
const user = await prisma.user.findUnique({ where: { id } }) | ||
if (user) { | ||
return done(null, user); | ||
} else { | ||
console.error( | ||
`Deserialize user failed: user with id` + ` ${id} does not exist` | ||
); | ||
return done(new Error("InvalidUser")); | ||
} | ||
}); | ||
|
||
passport.use( | ||
new LocalStrategy(async (username: string, password: string, done) => { | ||
const user = await prisma.user.findUnique({ where: { username: username } }) | ||
if (!user) { | ||
return done(new Error("InvalidUser")); | ||
} | ||
if (!bcrypt.compareSync(password, user.password)) { | ||
return done(new Error("InvalidUser")); | ||
} | ||
if (!user.confirmed && user.role !== "Student") { | ||
return done(new Error("UserNotConfirmed")); | ||
} | ||
return done(null, user); | ||
|
||
}), | ||
); | ||
|
||
|
||
const loginStrategy = new LocalStrategy( | ||
{ usernameField: "login" }, | ||
async (login, password, done) => { | ||
|
||
const user = await prisma.user.findUnique({ | ||
where: { | ||
OR: [{ email: login }, { username: login, }] | ||
} | ||
}); | ||
|
||
if (!user) { | ||
return Promise.resolve(done(new Error("InvalidUser"))); | ||
} | ||
if (!bcrypt.compareSync(password, user.password)) { | ||
console.error(`Login failed for user ${user.username}: incorrect password`); | ||
return Promise.resolve(done(new Error("InvalidUser"))); | ||
} | ||
if (!user.confirmed && user.role !== "Student") { | ||
console.error(`Login failed: ${user.username} is not confirmed`); | ||
return Promise.resolve(done(new Error("UserNotConfirmed"))); | ||
} | ||
return Promise.resolve(done(null, user)); | ||
} | ||
); | ||
|
||
passport.use(SigninStrategy.LOGIN, loginStrategy); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.