Skip to content

at async dispatch (https://deno.land/x/oak@v9.0.1/middleware.ts:41:7) #417

@goodmanworld

Description

@goodmanworld

I get this error message when I try to test the /api/register end point with Postman:

request: { url: "http://0.0.0.0:8000/api/register", method: "POST", hasBody: true }
response: { status: 404, type: undefined, hasBody: false, writable: true }

    at register (file:///C:/Users/m/app_back/controllers/auth_controller.ts:9:22)
    at async dispatch (https://deno.land/x/oak@v9.0.1/middleware.ts:41:7)
    at async dispatch (https://deno.land/x/oak@v9.0.1/middleware.ts:41:7)
    at async dispatch (https://deno.land/x/oak@v9.0.1/middleware.ts:41:7)
    at async EventTarget.#handleRequest (https://deno.land/x/oak@v9.0.1/application.ts:379:9)
TypeError: Cannot read properties of undefined (reading 'name')
    at register (file:///C:/Users/m/app_back/controllers/auth_controller.ts:9:22)
    at async dispatch (https://deno.land/x/oak@v9.0.1/middleware.ts:41:7)
    at async dispatch (https://deno.land/x/oak@v9.0.1/middleware.ts:41:7)
    at async dispatch (https://deno.land/x/oak@v9.0.1/middleware.ts:41:7)
    at async EventTarget.#handleRequest (https://deno.land/x/oak@v9.0.1/application.ts:379:9)


```This is my `auth_controller.ts` file:

import {
  create, verify, decode, getNumericDate, RouterContext, hashSync, compareSync
} from "../deps.ts";
import { userCollection } from "../mongo.ts";
import User from "../models/user.ts";

export class AuthController {
  async register(ctx: RouterContext) {
    const { value: { name, email, password } } = await ctx.request.body().value;

    let user = await User.findOne({ email });
    if (user) {
      ctx.response.status = 422;
      ctx.response.body = { message: "Email is already used" };
      return;
    }
    const hashedPassword = hashSync(password);
    user = new User({ name, email, password: hashedPassword });
    await user.save();
    ctx.response.status = 201;
    ctx.response.body = {
      id: user.id,
      name: user.name,
      email: user.email
    };
  }
  async login(ctx: RouterContext) {
    const { value: { email, password } } = await ctx.request.body().value;
    if (!email || !password) {
      ctx.response.status = 422;
      ctx.response.body = { message: "Please provide email and password" };
      return;
    }
    let user = await User.findOne({ email });
    if (!user) {
      ctx.response.status = 422;
      ctx.response.body = { message: "Incorrect email" };
      return;
    }
    if (!compareSync(password, user.password)) {
      ctx.response.status = 422;
      ctx.response.body = { message: "Incorrect password" };
      return;
    }

    const key = await crypto.subtle.generateKey(
      { name: "HMAC", hash: "SHA-512" },
      true,
      ["sign", "verify"],
    );

    const jwt = create( { 
      alg: "HS256",
      typ: "JWT",
    }, {
      iss: user.email,
      exp: getNumericDate(
        Date.now() + parseInt(Deno.env.get("JWT_EXP_DURATION") || "0"))
    },
    key
    );

    ctx.response.body = {
      id: user.id,
      name: user.name,
      email: user.email,
      jwt,
    };
  }
}

export default new AuthController();

What is the problem and how can I resolve it?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions