diff --git a/backend/src/apis/loginAPI.ts b/backend/src/apis/loginAPI.ts index 69f22cf92..b81316b3f 100644 --- a/backend/src/apis/loginAPI.ts +++ b/backend/src/apis/loginAPI.ts @@ -18,3 +18,19 @@ export const apiPost = async (url: string, payload: unknown): Promise => { return { success: false, result: error } } } + +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export const apiGet = async (url: string): Promise => { + try { + const result = await axios.get(url) + if (result.status !== 200) { + throw new Error('HTTP Status Error ' + result.status) + } + if (!['success', 'warning'].includes(result.data.state)) { + throw new Error(result.data.msg) + } + return { success: true, result } + } catch (error) { + return { success: false, result: error } + } +} diff --git a/backend/src/graphql/models/User.ts b/backend/src/graphql/models/User.ts index d17a3dacd..938d0a9f9 100644 --- a/backend/src/graphql/models/User.ts +++ b/backend/src/graphql/models/User.ts @@ -83,3 +83,15 @@ export class LoginResponse extends BaseEntity { @Column() user: User } + +@Entity() +@ObjectType() +export class LoginViaVerificationCode extends BaseEntity { + @Field(() => Number) + @Column() + sessionId: number + + @Field(() => String) + @Column() + email: string +} diff --git a/backend/src/graphql/resolvers/UserResolver.ts b/backend/src/graphql/resolvers/UserResolver.ts index 85571c5ab..34ceae7c7 100644 --- a/backend/src/graphql/resolvers/UserResolver.ts +++ b/backend/src/graphql/resolvers/UserResolver.ts @@ -1,11 +1,9 @@ // import jwt from 'jsonwebtoken' import { Resolver, Query, /* Mutation, */ Args, Arg } from 'type-graphql' import CONFIG from '../../config' -import { LoginResponse } from '../models/User' +import { LoginResponse, LoginViaVerificationCode } from '../models/User' import { UnsecureLoginArgs } from '../inputs/LoginUserInput' -import { apiPost } from '../../apis/loginAPI' -// import { CreateBookInput } from '../inputs/CreateBookInput' -// import { UpdateBookInput } from '../inputs/UpdateBookInput' +import { apiPost, apiGet } from '../../apis/loginAPI' @Resolver() export class UserResolver { @@ -43,16 +41,6 @@ export class UserResolver { }, } - // forgot password request - /* - @Query(() => null) - // eslint-disable-next-line @typescript-eslint/no-explicit-any - async sendEmail(@Arg() email: String): Promise { - const result = await apiPost(CONFIG.LOGIN_API_URL + 'sendEmail', { email }) - return - } - */ - // 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. @@ -67,20 +55,33 @@ export class UserResolver { // return loginResult.user ? loginResult.user : new User() } - /* - @Mutation(() => User) - async createBook(@Arg('data') data: CreateBookInput) { - const book = User.create(data) - await book.save() - return book + // forgot password request + @Query(() => String) + // eslint-disable-next-line @typescript-eslint/no-explicit-any + async sendEmail(@Arg('email') email: string): Promise { + const payload = { + email, + email_text: 7, + email_verification_code_type: 'resetPassword', + } + const result = await apiPost(CONFIG.LOGIN_API_URL + 'sendEmail', payload) + if (result.success) return result.result.data.state + return result.result } - */ - /* @Mutation(() => Boolean) - async deleteUser(@Arg('id') id: string): Promise { - const user = await User.findOne({ where: { id } }) - if (!user) throw new Error('User not found!') - await user.remove() - return true - } */ + @Query(() => LoginViaVerificationCode) + // eslint-disable-next-line @typescript-eslint/no-explicit-any + async loginViaEmailVerificationCode(@Arg('optin') optin: string): Promise { + // I cannot use number as type here. + // The value received is not the same as sent by the query + const result = await apiGet( + CONFIG.LOGIN_API_URL + 'loginViaEmailVerificationCode?emailVerificationCode=' + optin, + ) + if (result.success) + return { + sessionId: result.result.data.session_id, + email: result.result.data.user.email, + } + return result.result + } }