benawad / benawad/jwt-auth-example

Little bit of issue

Open
#19 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
463
Forks
119
PR merge metrics
No merged PRs in 30d

Description

UserRevolver.ts
```typescript
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
{
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
```typescript
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
```typescript
Type 'Promise' is not assignable to type 'string | (() => string) | undefined'.
Type 'Promise' is not assignable to type '() => string'.
Type 'Promise' 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

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in UserResolver.ts at register and login, then inspect User.ts and the password value passed through User.insert. Reproduce the flow in GraphQL Playground by registering and logging in with the same credentials, and compare the stored password with the value consumed by login. Done means registration and subsequent login succeed without the bad-password error.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api, authentication, backend, security
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.