Skip to content

Little bit of issue  #19

@SandroB07

Description

@SandroB07

UserRevolver.ts

import {Resolver, Query, Mutation, Arg, ObjectType, Field} from 'type-graphql'
import {hash, compare} from 'bcryptjs';
import {sign} from 'jsonwebtoken'
import {User} from "./entity/User"


@ObjectType()
class LoginResponse {
	@Field()
	accessToken: string;
}

@Resolver()
export class UserResolver {
	@Query(() => String)
	hello() {
		return 'hi!';
	}  

	@Query(() => [User])
	users() {
		return User.find();
	}  

	@Mutation(() => Boolean)
	async register(
		@Arg("email") email: string,
		@Arg("password") password: string
	) 
	{
		const hashedPassword = hash(password, 12);

		try {		
			await User.insert({
				email,
				password: hashedPassword
			});	
			
			return true;
		} catch (err) {
			console.log(err);
			return false;
		}
	}

	@Mutation(() => LoginResponse)
	async login(@Arg('email') email: string,@Arg('password') password: string): Promise<LoginResponse>
	{
		const user = await User.findOne({ where: { email } });

		if (!user) {
			throw new Error('could not find user');
		}

		const valid = await compare(password, user.password);

		if (!valid) {
			throw new Error('bad password');
		}		

		// Login succesful	

		return {
			accessToken: sign({userId: user.id}, 'aHhslhjksdsald', {expiresIn: "15m"})
		}
	}	
}

User.ts

import {Entity, PrimaryGeneratedColumn, Column, BaseEntity} from "typeorm";
import {ObjectType, Field, Int} from 'type-graphql'

@ObjectType()
@Entity("users")
export class User extends BaseEntity {
	@Field(() => Int)
    @PrimaryGeneratedColumn()
    id: number;

    @Field()
	@Column("text")
    email: string;

    @Column("text")
    password: string;
	
	//static insert: any;
}

myError

Type 'Promise<string>' is not assignable to type 'string | (() => string) | undefined'.
  Type 'Promise<string>' is not assignable to type '() => string'.
    Type 'Promise<string>' provides no match for the signature '(): string'.

(property) password?: string | (() => string) | undefined

Hello Ben,

I'm following your tutorial on and have a problem which I didn't manage to resolve :

(myError) is highlited in UserResolve.ts -> register function -> in try {} block on password. I'm working in sublime on ubuntu and I opened everything with VScode to have better error message which said that I didn't have insert defined in User so that's kinda what I did, I have it commented out now It seemed to work but another problem appeared: in graphsql playground whenever I register a user I can't get an accessToken => every time I get "bad password". I think the problem comes from static insert: any , but really can't figure out. If you could help me it would be amazing.

Thanks in advance!!

SandroB

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions