Code Monkey home page Code Monkey logo

Comments (7)

panva avatar panva commented on August 25, 2024

Can you post a full reproduction script?

from jose.

alan19031216 avatar alan19031216 commented on August 25, 2024

Coding

import express, { Request, Response } from "express";
import { body } from "express-validator";
import { validateRequest, BadRequestError } from "sansction-common";
import jose from 'jose'

import { User } from "../models/user";
import { Password } from "../services/password";

const router = express.Router();

router.post(
    "/api/users/signin",
    [
        body("email").isEmail().withMessage("Email must be valid"),
        body("password")
            .trim()
            .notEmpty()
            .isLength({ min: 4, max: 20 })
            .withMessage("Password must be between 4 and 20 characters long"),
    ],
    validateRequest,
    async (req: Request, res: Response) => {
        const { email, password } = req.body;

        const user = await User.findOne({ email });
        if (!user) {
            throw new BadRequestError("Email not found", 404);
        }

        const passwordsMatch = await Password.compare(
            user.password,
            password
        );
        if (!passwordsMatch) {
            throw new BadRequestError("Password not match", 401);
        }

        // Generate JWT
        const secret = jose.base64url.decode(user.salt)
        const userJWT = await new jose.EncryptJWT({ id: user.id, email: user.email })
            .setExpirationTime('2h')
            .encrypt(secret)


        res.send({ 'accessToken': userJWT });
    }
);

export { router as signinRouter };

Error msg

TypeError: Cannot read properties of undefined (reading 'base64url')
at D:\sanction-api\auth\src\routes\signin.ts:40:29
at Generator.next ()
at fulfilled (D:\sanction-api\auth\src\routes\signin.ts:5:58)
at processTicksAndRejections (node:internal/process/task_queues:96:5)

from jose.

panva avatar panva commented on August 25, 2024

Please see the README on how to import the module.

In ESM you're supposed to

import * as jose from 'jose'

from jose.

panva avatar panva commented on August 25, 2024

Also, node version 16.20.1 is no longer supported on v5.x of jose, see CHANGELOG.md

from jose.

alan19031216 avatar alan19031216 commented on August 25, 2024

alright. thank you

from jose.

alan19031216 avatar alan19031216 commented on August 25, 2024

import * as jose from 'jose

Also, node version 16.20.1 is no longer supported on v5.x of jose, see CHANGELOG.md

I just tried in Node V16.20.1. So far looks good. I still need upgrade my node version?

from jose.

panva avatar panva commented on August 25, 2024

Yeah, Node.js v16.x is End-Of-Life since September 11th 2023.

from jose.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.