diff --git a/backend/package.json b/backend/package.json index 02328230f..16b0cdf22 100644 --- a/backend/package.json +++ b/backend/package.json @@ -12,7 +12,7 @@ "lint": "eslint . --ext .js,.ts" }, "dependencies": { - "apollo-datasource-rest": "^0.14.0", + "axios": "^0.21.1", "express": "^4.17.1", "express-graphql": "^0.12.0", "graphql": "^15.5.1", diff --git a/backend/src/apis/loginAPI.ts b/backend/src/apis/loginAPI.ts new file mode 100644 index 000000000..7370ee50c --- /dev/null +++ b/backend/src/apis/loginAPI.ts @@ -0,0 +1,163 @@ +import axios from 'axios' +import CONFIG from '../config' +// eslint-disable-next-line no-unused-vars +import regeneratorRuntime from 'regenerator-runtime' + +// control email-text sended with email verification code +const EMAIL_TYPE = { + DEFAULT: 2, // if user has registered directly + ADMIN: 5, // if user was registered by an admin +} + +const apiGet = async (url: string) => { + 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 } + } +} + +const apiPost = async (url: string, payload: string) => { + try { + const result = await axios.post(url, payload) + if (result.status !== 200) { + throw new Error('HTTP Status Error ' + result.status) + } + if (result.data.state === 'warning') { + return { success: true, result: result.error } + } + if (result.data.state !== 'success') { + throw new Error(result.data.msg) + } + return { success: true, result } + } catch (error) { + return { success: false, result: error } + } +} + +const loginAPI = { + login: async (email: string, password: string): Promise => { + const payload: any = { + email, + password, + } + return apiPost(CONFIG.LOGIN_API_URL + 'unsecureLogin', payload) + }, + logout: async (sessionId: number): Promise => { + const payload: any = { session_id: sessionId } + return apiPost(CONFIG.LOGIN_API_URL + 'logout', payload) + }, + create: async ( + email: string, + firstName: string, + lastName: string, + password: string, + ): Promise => { + const payload: any = { + email, + first_name: firstName, + last_name: lastName, + password, + emailType: EMAIL_TYPE.DEFAULT, + login_after_register: true, + } + return apiPost(CONFIG.LOGIN_API_URL + 'createUser', payload) + }, + sendEmail: async ( + email: string, + email_text = 7, + email_verification_code_type = 'resetPassword', + ): Promise => { + const payload: any = { + email, + email_text, + email_verification_code_type, + } + return apiPost(CONFIG.LOGIN_API_URL + 'sendEmail', payload) + }, + loginViaEmailVerificationCode: async (optin: number): Promise => { + return apiGet( + CONFIG.LOGIN_API_URL + 'loginViaEmailVerificationCode?emailVerificationCode=' + optin, + ) + }, + getUserInfos: async (sessionId: number, email: string): Promise => { + const payload: any = { + session_id: sessionId, + email: email, + ask: ['user.first_name', 'user.last_name'], + } + return apiPost(CONFIG.LOGIN_API_URL + 'getUserInfos', payload) + }, + updateUserInfos: async (sessionId: number, email: string, data: any): Promise => { + const payload: any = { + session_id: sessionId, + email, + update: { + 'User.first_name': data.firstName, + 'User.last_name': data.lastName, + 'User.description': data.description, + }, + } + return apiPost(CONFIG.LOGIN_API_URL + 'updateUserInfos', payload) + }, + changePassword: async (sessionId: number, email: string, password: string): Promise => { + const payload: any = { + session_id: sessionId, + email, + password, + } + return apiPost(CONFIG.LOGIN_API_URL + 'resetPassword', payload) + }, + changePasswordProfile: async ( + sessionId: number, + email: string, + password: string, + passwordNew: string, + ): Promise => { + const payload: any = { + session_id: sessionId, + email, + update: { + 'User.password_old': password, + 'User.password': passwordNew, + }, + } + return apiPost(CONFIG.LOGIN_API_URL + 'updateUserInfos', payload) + }, + changeUsernameProfile: async ( + sessionId: number, + email: string, + username: string, + ): Promise => { + const payload: any = { + session_id: sessionId, + email, + update: { + 'User.username': username, + }, + } + return apiPost(CONFIG.LOGIN_API_URL + 'updateUserInfos', payload) + }, + updateLanguage: async (sessionId: number, email: string, language: string): Promise => { + const payload: any = { + session_id: sessionId, + email, + update: { + 'User.language': language, + }, + } + return apiPost(CONFIG.LOGIN_API_URL + 'updateUserInfos', payload) + }, + checkUsername: async (username: string, groupId = 1): Promise => { + return apiGet(CONFIG.LOGIN_API_URL + `checkUsername?username=${username}&group_id=${groupId}`) + }, +} + +export default loginAPI diff --git a/backend/src/graphql/datasources/loginServer.ts b/backend/src/graphql/datasources/loginServer.ts deleted file mode 100644 index ff802bce8..000000000 --- a/backend/src/graphql/datasources/loginServer.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { RESTDataSource } from 'apollo-datasource-rest' -import CONFIG from '../../config' -import { Group } from '../models/Group' - -export class LoginServerAPI extends RESTDataSource { - constructor() { - super() - this.baseURL = CONFIG.LOGIN_API_URL - } - - async getAllGroupAliases(): Promise { - return new Promise(() => { - const response = await this.post('networkInfos', { ask: ['groups'] }) - const groups: Group[] = [] - response.data.forEach((element: string) => { - const group = new Group() - group.alias = element - groups.push(group) - })(groups) - return groups - }) - } -}