From 8443888e964c70662e1d818df6e0a460a2cd226f Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Tue, 13 Jul 2021 18:34:50 +0200 Subject: [PATCH] login resolver returns data that the frontend could use without major changes --- backend/.env.dist | 2 +- backend/src/graphql/models/User.ts | 85 +++++++++++++++++++ backend/src/graphql/resolvers/BookResolver.ts | 2 + backend/src/graphql/resolvers/UserResolver.ts | 35 +++++--- 4 files changed, 113 insertions(+), 11 deletions(-) create mode 100644 backend/src/graphql/models/User.ts diff --git a/backend/.env.dist b/backend/.env.dist index 21127b9ed..fe9d5e566 100644 --- a/backend/.env.dist +++ b/backend/.env.dist @@ -1,4 +1,4 @@ PORT=4000 GRAPHIQL=false -// LOGIN_API_URL=http://localhost/login_api/ +// LOGIN_API_URL=http://login-server:1201/ // COMMUNITY_API_URL=http://localhost/api/ \ No newline at end of file diff --git a/backend/src/graphql/models/User.ts b/backend/src/graphql/models/User.ts new file mode 100644 index 000000000..d17a3dacd --- /dev/null +++ b/backend/src/graphql/models/User.ts @@ -0,0 +1,85 @@ +import { Entity, BaseEntity, Column } from 'typeorm' +import { ObjectType, Field } from 'type-graphql' + +@Entity() +@ObjectType() +export class User extends BaseEntity { + /* + @Field(() => ID) + @PrimaryGeneratedColumn() + id: number + */ + + @Field(() => String) + @Column({ length: 191 }) + email: string + + @Field(() => String) + @Column({ length: 150 }) + firstName: string + + @Field(() => String) + @Column() + lastName: string + + @Field(() => String) + @Column() + username: string + + @Field(() => String) + @Column('text') + description: string + + /* + @Field(() => String) + @Column({ length: 64 }) + pubkey: string + + // not sure about the type here. Maybe better to have a string + @Field(() => number) + @Column({ type: 'datetime' }) + created: number + + @Field(() => Boolean) + @Column({ default: false }) + emailChecked: boolean + + @Field(() => Boolean) + @Column({ default: false }) + passphraseShown: boolean + */ + + @Field(() => String) + @Column({ default: 'de' }) + language: string + + /* + @Field(() => Boolean) + @Column({ default: false }) + disabled: boolean + */ + + /* I suggest to have a group as type here + @Field(() => ID) + @Column() + groupId: number + + // what is puvlisherId? + @Field(() => ID) + @Column({ default: 0 }) + publisherId: number + */ +} + +// temporaray solution until we have JWT implemented +@Entity() +@ObjectType() +export class LoginResponse extends BaseEntity { + @Field(() => Number) + @Column() + sessionId: number + + @Field(() => User) + @Column() + user: User +} diff --git a/backend/src/graphql/resolvers/BookResolver.ts b/backend/src/graphql/resolvers/BookResolver.ts index 9e5b6d1b3..df7e553bc 100644 --- a/backend/src/graphql/resolvers/BookResolver.ts +++ b/backend/src/graphql/resolvers/BookResolver.ts @@ -1,3 +1,4 @@ +/* import { Resolver, Query, Mutation, Arg } from 'type-graphql' import { Book } from '../models/Book' import { CreateBookInput } from '../inputs/CreateBookInput' @@ -39,3 +40,4 @@ export class BookResolver { return true } } +*/ diff --git a/backend/src/graphql/resolvers/UserResolver.ts b/backend/src/graphql/resolvers/UserResolver.ts index f296dc176..959c101e8 100644 --- a/backend/src/graphql/resolvers/UserResolver.ts +++ b/backend/src/graphql/resolvers/UserResolver.ts @@ -1,18 +1,18 @@ -import jwt from 'jsonwebtoken' +// import jwt from 'jsonwebtoken' import axios from 'axios' import { Resolver, Query, /* Mutation, */ Arg } from 'type-graphql' import CONFIG from '../../config' -// import { User } from '../models/User' +import { LoginResponse } from '../models/User' // import { LoginUserInput } from '../inputs/LoginUserInput' // import { loginAPI, LoginResult } from '../../apis/loginAPI' // import { CreateBookInput } from '../inputs/CreateBookInput' // import { UpdateBookInput } from '../inputs/UpdateBookInput' -const apiPost = async (url: string, payload: any): Promise => { +const apiPost = async (url: string, payload: unknown): Promise => { try { - console.log(url, payload) + // console.log(url, payload) const result = await axios.post(url, payload) - console.log('-----', result) + // console.log('-----', result) if (result.status !== 200) { throw new Error('HTTP Status Error ' + result.status) } @@ -24,7 +24,7 @@ const apiPost = async (url: string, payload: any): Promise => { } return { success: true, result } } catch (error) { - console.log(error) + // console.log(error) return { success: false, result: error } } } @@ -41,10 +41,9 @@ export class UserResolver { return User.findOne({ where: { id } }) } */ - @Query(() => String) - async login(@Arg('email') email: string, @Arg('password') password: string): Promise { + @Query(() => LoginResponse) + async login(@Arg('email') email: string, @Arg('password') password: string): Promise { email = email.trim().toLowerCase() - console.log(email, password, CONFIG.LOGIN_API_URL) const result = await apiPost(CONFIG.LOGIN_API_URL + 'unsecureLogin', { email, password }) // if there is no user, throw an authentication error @@ -52,13 +51,29 @@ export class UserResolver { throw new Error(result.result) } + // temporary solution until we have JWT implemented + // console.log(result.result.data) + return { + sessionId: result.result.data.session_id, + user: { + email: result.result.data.user.email, + language: result.result.data.user.language, + username: result.result.data.user.username, + firstName: result.result.data.user.first_name, + lastName: result.result.data.user.last_name, + description: result.result.data.user.description, + }, + } + // create and return the json web token // The expire doesn't help us here. The client needs to track when the token expires on its own, // since every action prolongs the time the session is valid. + /* return jwt.sign( { result, role: 'todo' }, - CONFIG.JWT_SECRET /* , { expiresIn: CONFIG.JWT_EXPIRES_IN } */, + CONFIG.JWT_SECRET, // * , { expiresIn: CONFIG.JWT_EXPIRES_IN } , ) + */ // return (await apiPost(CONFIG.LOGIN_API_URL + 'unsecureLogin', login)).result.data // const loginResult: LoginResult = await loginAPI.login(data) // return loginResult.user ? loginResult.user : new User()