Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 views
in Technique[技术] by (71.8m points)

typescript - My session is not initializing using express-session and redis

Please help me make my session initialize. I'm using express-session connect-redis and ioredis to store my sessions and typegraphql as my graphql API framework.

MyContext.ts

interface MyContext {
  req: Request & {
    session: {
      userId: number;
    };
  };

LoginResolver.ts

import {User} from "../entities/User";
import { Arg, Ctx, Mutation, Query, Resolver } from "type-graphql";
import MyContext from "../MyContext";

@Resolver()
class LoginResolver {
    @Mutation(() => Boolean)
    async login(
        @Arg("username") username: string,
        @Arg("password") password: string,
        @Ctx() ctx: MyContext
    ) {
        const user = await User.findOne({ username: username});

        if(!user)
            return false;

        const vaild = user.password == password;

        if(!vaild)
            return false;

        
        ctx.req.session.userId = user.id;

        return true;
    }
}

When I use saveUninitialized: true the session is stored but when I modify it in LoginResolver it doesn't work thus if saveUninitialized: false the session is not initializing.

question from:https://stackoverflow.com/questions/65645422/my-session-is-not-initializing-using-express-session-and-redis

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...