-
-
Notifications
You must be signed in to change notification settings - Fork 495
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug]: validateSession return null in Express Mongoose #1668
Comments
Related issue: #1681 |
Note whether you are using _id or id: existingUser._id and existingUser.id are different things. When creating a user in the database, don't generate the _id yourself. (Just my opinion) Everything is working for me. |
i'm not generating the id myself; mongoose has a shortcut so I can use either id or _id , and using only User._id doesn't fix the problem for me |
@issam-seghir Can you show me how you create a session? So that I can understand better. |
export const loginHandler = asyncWrapper(async (req: Request, res: Response, next: NextFunction) => {
const {
body: { email, password },
} = await zParse(loginSchema, req, res);
const user = await User.findOne({ email }).exec();
if (!user) {
return res.status(400).json({ error: "User not Found" });
}
const isPasswordValid = await user.isPasswordMatch(password);
if (!isPasswordValid) {
return res.status(400).json({ error: "Incorrect password" });
}
const session = await lucia.createSession(user.id, {});
res.setHeader("Set-Cookie", lucia.createSessionCookie(session.id).serialize());
res.status(200).json({
success: `Login : ${user.fullName}!`,
data: {
user: user,
session: session,
},
});
const router = Router();
router.use("/auth", authRouter);
router.use(verifyRequestOriginMiddleware);
router.use(sessionValidationMiddleware);
//* Protected routes
router.use("/users", usersRouter);
router.use("/messages", messagesRouter);
...
export default router;
import mongoose, { Document } from "mongoose";
export interface ISession extends Document {
user_id: string;
expires_at: Date;
}
export const SessionSchema = new mongoose.Schema<ISession>({
user_id: {
type: String,
required: true,
},
expires_at: {
type: Date,
required: true,
},
});
const Session = mongoose.model<ISession>("Session", SessionSchema);
export default Session;
const userSchema: Schema<IUser, IUserModel, IUserMethods, object> = new mongoose.Schema(
{
fullName: {
type: String,
required: true,
trim: true,
},
email: {
type: String,
// unique: true,
trim: true,
lowercase: true,
private: true,
},
password: {
type: String,
trim: true,
minlength: 8,
private: true,
},
role: {
type: String,
enum: ["user", "admin"],
default: "user",
},
.....
},
},
{
timestamps: true,
toJSON: { virtuals: true },
toObject: { virtuals: true },
}
);
//* Apply plugins
userSchema.plugin(toJSON);
userSchema.plugin(paginate); |
In the loginHandler function, the line |
the response is : {
"success": "Login : admin!",
"data": {
"user": {
"attachment": {
....
},
"fullName": "admin",
"role": "admin",
"id": "66d6e9ac2010a31b4d541580"
},
"session": {
"id": "fga7xlsupkdwpaz2rnlkqf6dvycsrhm7zfgev4qw",
"userId": "66d6e9ac2010a31b4d541580",
"fresh": true,
"expiresAt": "2024-10-09T11:16:17.696Z"
}
}
} |
Package
lucia
Describe the bug
when using the
sessionValidationMiddleware
, the await lucia.validateSession(sessionId) always returnnull
The text was updated successfully, but these errors were encountered: