-
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 delete projet and add session protection for admin
- Loading branch information
Showing
10 changed files
with
230 additions
and
96 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
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,80 @@ | ||
|
||
import { prisma } from "@celluloid/prisma" | ||
import { User } 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: User, done) => { | ||
done(null, user.id) | ||
}); | ||
|
||
passport.deserializeUser(async (id: string, done) => { | ||
const user = await prisma.user.findUnique({ where: { id } }) | ||
if (user) { | ||
console.log("here", 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, }] | ||
email: 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); | ||
|
||
export default passport; |
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,36 @@ | ||
import RedisStore from "connect-redis" | ||
import session from "express-session" | ||
import { createClient } from "redis" | ||
|
||
|
||
export function createSession() { | ||
|
||
// Initialize client. | ||
const redisClient = createClient({ | ||
url: process.env.REDIS_URL || "redis://localhost" | ||
}) | ||
redisClient.connect().catch(console.error) | ||
|
||
// Initialize store. | ||
const redisStore = new RedisStore({ | ||
client: redisClient, | ||
}) | ||
|
||
return session({ | ||
store: redisStore, | ||
name: process.env.CELLULOID_COOKIE_NAME | ||
? process.env.CELLULOID_COOKIE_NAME | ||
: undefined, | ||
cookie: { | ||
domain: process.env.CELLULOID_COOKIE_DOMAIN | ||
? process.env.CELLULOID_COOKIE_DOMAIN | ||
: undefined, | ||
secure: process.env.CELLULOID_COOKIE_SECURE === "true", | ||
maxAge: 30 * 24 * 3600 * 1000, | ||
httpOnly: true, | ||
}, | ||
secret: process.env.CELLULOID_COOKIE_SECRET as string, | ||
resave: false, | ||
saveUninitialized: true, | ||
}); | ||
} |
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.