diff --git a/backend/src/apis/loginAPI.ts b/backend/src/apis/loginAPI.ts index e99c9c729..d4f4e278e 100644 --- a/backend/src/apis/loginAPI.ts +++ b/backend/src/apis/loginAPI.ts @@ -23,9 +23,6 @@ export const apiGet = async (url: string): Promise => { return axios .get(url) .then((result) => { - // eslint-disable-next-line no-console - console.log('IN apiGet.THEN: ' + JSON.stringify({ success: true, result: result })) - if (result.status !== 200) { // eslint-disable-next-line no-console console.log('IN status: ' + 'HTTP Status Error ' + result.status) @@ -33,7 +30,7 @@ export const apiGet = async (url: string): Promise => { } if (!['success', 'warning'].includes(result.data.state)) { // eslint-disable-next-line no-console - console.log('IN state: ' + result.data.state + ' message: ' + result.data.msg) + console.log('IN state: ' + result.data.state + ' message: ' + JSON.stringify(result)) throw new Error(result.data.msg) } return { success: true, result: result } diff --git a/backend/src/graphql/inputs/LoginUserInput.ts b/backend/src/graphql/inputs/LoginUserInput.ts index 7edc3089e..8ae0552de 100644 --- a/backend/src/graphql/inputs/LoginUserInput.ts +++ b/backend/src/graphql/inputs/LoginUserInput.ts @@ -25,3 +25,87 @@ export class UnsecureLoginArgs { @Field(() => String) password: string } + +@ArgsType() +export class CreateUserArgs { + @Field(() => String) + email: string + + @Field(() => String) + firstName: string + + @Field(() => String) + lastName: string + + @Field(() => String) + password: string +} + +@ArgsType() +export class SendEmailArgs { + @Field(() => String) + email: string + + @Field(() => Number) + emailText: number + + @Field(() => String) + emailVerificationCodeType: string +} + +@ArgsType() +export class GetUserInfoArgs { + @Field(() => Number) + sessionId: number + + @Field(() => String) + email: string +} + +@ArgsType() +export class ChangePasswordArgs { + @Field(() => Number) + sessionId: number + + @Field(() => String) + email: string + + @Field(() => String) + password: string +} + +@ArgsType() +export class UpdateUserInfosArgs { + @Field(() => Number) + sessionId!: number + + @Field(() => String) + email!: string + + @Field(() => String) + firstName?: string + + @Field(() => String) + lastName?: string + + @Field(() => String) + username?: string + + @Field(() => String) + language?: string + + @Field(() => String) + password?: string + + @Field(() => String) + passwordNew?: string +} + +@ArgsType() +export class CheckUsernameArgs { + @Field(() => String) + username: string + + @Field(() => Number) + groupId?: number +} \ No newline at end of file diff --git a/backend/src/graphql/models/User.ts b/backend/src/graphql/models/User.ts index 938d0a9f9..394d6d3f4 100644 --- a/backend/src/graphql/models/User.ts +++ b/backend/src/graphql/models/User.ts @@ -95,3 +95,104 @@ export class LoginViaVerificationCode extends BaseEntity { @Column() email: string } + +@Entity() +@ObjectType() +export class LogoutResponse extends BaseEntity { + @Field(() => String) + state: string +} + +@Entity() +@ObjectType() +export class CreateResponse extends BaseEntity { + @Field(() => String) + state: string +} + +@Entity() +@ObjectType() +export class SendEmailResponse extends BaseEntity { + @Field(() => String) + state: string + + @Field(() => String) + msg?: string +} + +@Entity() +@ObjectType() +export class Server extends BaseEntity { + @Field(() => String) + loginServerPath: string +} + +@Entity() +@ObjectType() +export class ErrorData extends BaseEntity { + @Field(() => String) + messages: string +} + +@Entity() +@ObjectType() +export class GetUserInfoResponse extends BaseEntity { + /* "state": "success", + "userData": { + "EmailVerificationCode.Register": "2718271129122", + "pubkeyhex": "131c7f68dd94b2be4c913400ff7ff4cdc03ac2bda99c2d29edcacb3b065c67e6", + "first_name": "Max", + "last_name": "Musterman", + "disabled": 0, + "email_checked": 1 + }, + "server": { + "loginServer.path": "http://localhost/account" + }, + "errors": [] + */ + @Field(() => String) + state: string + + @Field(() => User) + userData: User + + @Field(() => Server) + server: Server + + @Field(() => [ErrorData]) + errors: [ErrorData] +} + +@Entity() +@ObjectType() +export class ChangePasswordResponse extends BaseEntity { + @Field(() => String) + state: string +} + +@Entity() +@ObjectType() +export class UpdateUserInfosResponse extends BaseEntity { + @Field(() => String) + state: string + + @Field(() => Number) + validValues: number + + @Field(() => [ErrorData]) + errors: [ErrorData] +} + +@Entity() +@ObjectType() +export class CheckUsernameResponse extends BaseEntity { + @Field(() => String) + state: string + + @Field(() => String) + msg?: string + + @Field(() => Number) + groupId?: number +} diff --git a/backend/src/graphql/resolvers/UserResolver.ts b/backend/src/graphql/resolvers/UserResolver.ts index 34ceae7c7..31e983f99 100644 --- a/backend/src/graphql/resolvers/UserResolver.ts +++ b/backend/src/graphql/resolvers/UserResolver.ts @@ -1,22 +1,30 @@ // import jwt from 'jsonwebtoken' import { Resolver, Query, /* Mutation, */ Args, Arg } from 'type-graphql' import CONFIG from '../../config' -import { LoginResponse, LoginViaVerificationCode } from '../models/User' -import { UnsecureLoginArgs } from '../inputs/LoginUserInput' +import { + ChangePasswordResponse, + CheckUsernameResponse, + CreateResponse, + GetUserInfoResponse, + LoginResponse, + LoginViaVerificationCode, + LogoutResponse, + SendEmailResponse, + UpdateUserInfosResponse, +} from '../models/User' +import { + ChangePasswordArgs, + CheckUsernameArgs, + CreateUserArgs, + GetUserInfoArgs, + SendEmailArgs, + UnsecureLoginArgs, + UpdateUserInfosArgs, +} from '../inputs/LoginUserInput' import { apiPost, apiGet } from '../../apis/loginAPI' @Resolver() export class UserResolver { - /* @Query(() => [User]) - users(): Promise { - return User.find() - } */ - - /* @Query(() => User) - user(@Arg('id') id: string): Promise { - return User.findOne({ where: { id } }) - } */ - @Query(() => LoginResponse) // eslint-disable-next-line @typescript-eslint/no-explicit-any async login(@Args() { email, password }: UnsecureLoginArgs): Promise { @@ -55,20 +63,6 @@ export class UserResolver { // return loginResult.user ? loginResult.user : new User() } - // 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 - } - @Query(() => LoginViaVerificationCode) // eslint-disable-next-line @typescript-eslint/no-explicit-any async loginViaEmailVerificationCode(@Arg('optin') optin: string): Promise { @@ -84,4 +78,92 @@ export class UserResolver { } return result.result } + + @Query(() => LogoutResponse) + async logout(@Arg('sessionId') sessionId: number): Promise { + const payload = { session_id: sessionId } + const result = apiPost(CONFIG.LOGIN_API_URL + 'logout', payload); + return result + } + + @Query(() => CreateResponse) + async create(@Args() { email, firstName, lastName, password }: CreateUserArgs): Promise { + const payload = { + email, + first_name: firstName, + last_name: lastName, + password, + emailType: 2, + login_after_register: true, + } + return apiPost(CONFIG.LOGIN_API_URL + 'createUser', payload) + } + + @Query(() => SendEmailResponse) + async sendEmail( + @Args() + { email, emailText = 7, emailVerificationCodeType = 'resetPassword' }: SendEmailArgs, + ): Promise { + const payload = { + email, + email_text: emailText, + email_verification_code_type: emailVerificationCodeType, + } + return apiPost(CONFIG.LOGIN_API_URL + 'sendEmail', payload) + } + + @Query(() => GetUserInfoResponse) + async getUserInfos(@Args() { sessionId, email }: GetUserInfoArgs): Promise { + const payload = { + session_id: sessionId, + email: email, + ask: ['user.first_name', 'user.last_name'], + } + return apiPost(CONFIG.LOGIN_API_URL + 'getUserInfos', payload) + } + + @Query(() => ChangePasswordResponse) + async changePassword(@Args() { sessionId, email, password }: ChangePasswordArgs): Promise { + const payload = { + session_id: sessionId, + email, + password, + } + return apiPost(CONFIG.LOGIN_API_URL + 'resetPassword', payload) + } + + @Query(() => UpdateUserInfosResponse) + async updateUserInfos( + @Args() + { + sessionId, + email, + firstName, + lastName, + username, + language, + password, + passwordNew, + }: UpdateUserInfosArgs, + ): Promise { + const payload = { + session_id: sessionId, + email, + update: { + 'User.first_name': firstName, + 'User.last_name': lastName, + // 'User.description': data.description, + 'User.username': username, + 'User.language': language, + 'User.password_old': password, + 'User.password': passwordNew, + }, + } + return apiPost(CONFIG.LOGIN_API_URL + 'updateUserInfos', payload) + } + + @Query(() => CheckUsernameResponse) + async checkUsername(@Args() { username, groupId = 1 }: CheckUsernameArgs): Promise { + return apiGet(CONFIG.LOGIN_API_URL + `checkUsername?username=${username}&group_id=${groupId}`) + } }