login resolver returns data that the frontend could use without major changes

This commit is contained in:
Moriz Wahl 2021-07-13 18:34:50 +02:00
parent b6d51145df
commit 8443888e96
4 changed files with 113 additions and 11 deletions

View File

@ -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/

View File

@ -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
}

View File

@ -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
}
}
*/

View File

@ -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<any> => {
const apiPost = async (url: string, payload: unknown): Promise<unknown> => {
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<any> => {
}
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<string> {
@Query(() => LoginResponse)
async login(@Arg('email') email: string, @Arg('password') password: string): Promise<unknown> {
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()