mirror of
https://github.com/Paris-est-Ludique/intranet.git
synced 2025-09-11 13:56:29 +02:00
Add login api
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import axios from "axios"
|
||||
import _ from "lodash"
|
||||
|
||||
import config from "../config"
|
||||
import { axiosConfig } from "./auth"
|
||||
@@ -12,7 +11,7 @@ export default function getServiceAccessors<
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
ElementNoId extends object,
|
||||
Element extends ElementNoId & ElementWithId
|
||||
>(elementName: string, translation: { [k in keyof Element]: string }): any {
|
||||
>(elementName: string): any {
|
||||
function get(): (id: number) => Promise<{
|
||||
data?: Element
|
||||
error?: Error
|
||||
@@ -27,14 +26,7 @@ export default function getServiceAccessors<
|
||||
...axiosConfig,
|
||||
params: { id },
|
||||
})
|
||||
if (!data) {
|
||||
return { data }
|
||||
}
|
||||
const englishData = _.mapValues(
|
||||
translation,
|
||||
(frenchProp: string) => data[frenchProp]
|
||||
) as Element
|
||||
return { data: englishData }
|
||||
return { data }
|
||||
} catch (error) {
|
||||
return { error: error as Error }
|
||||
}
|
||||
@@ -55,18 +47,7 @@ export default function getServiceAccessors<
|
||||
`${config.API_URL}/${elementName}ListGet`,
|
||||
axiosConfig
|
||||
)
|
||||
if (!data) {
|
||||
return { data }
|
||||
}
|
||||
|
||||
const englishDataList = data.map(
|
||||
(frenchData: any) =>
|
||||
_.mapValues(
|
||||
translation,
|
||||
(frenchProp: string) => frenchData[frenchProp]
|
||||
) as Element
|
||||
)
|
||||
return { data: englishDataList }
|
||||
return { data }
|
||||
} catch (error) {
|
||||
return { error: error as Error }
|
||||
}
|
||||
@@ -84,27 +65,12 @@ export default function getServiceAccessors<
|
||||
}
|
||||
return async (volunteerWithoutId: ElementNoId): Promise<ElementGetResponse> => {
|
||||
try {
|
||||
const invertedTranslationWithoutId = _.invert(_.omit(translation, "id"))
|
||||
const frenchDataWithoutId = _.mapValues(
|
||||
invertedTranslationWithoutId,
|
||||
(englishProp: string, _frenchProp: string) =>
|
||||
(volunteerWithoutId as any)[englishProp]
|
||||
)
|
||||
|
||||
const { data } = await axios.post(
|
||||
`${config.API_URL}/${elementName}Add`,
|
||||
frenchDataWithoutId,
|
||||
volunteerWithoutId,
|
||||
axiosConfig
|
||||
)
|
||||
if (!data) {
|
||||
return { data }
|
||||
}
|
||||
|
||||
const englishData = _.mapValues(
|
||||
translation,
|
||||
(frenchProp: string) => data[frenchProp]
|
||||
) as Element
|
||||
return { data: englishData }
|
||||
return { data }
|
||||
} catch (error) {
|
||||
return { error: error as Error }
|
||||
}
|
||||
@@ -121,26 +87,12 @@ export default function getServiceAccessors<
|
||||
}
|
||||
return async (volunteer: Element): Promise<ElementGetResponse> => {
|
||||
try {
|
||||
const invertedTranslation = _.invert(translation)
|
||||
const frenchData = _.mapValues(
|
||||
invertedTranslation,
|
||||
(englishProp: string) => (volunteer as any)[englishProp]
|
||||
)
|
||||
|
||||
const { data } = await axios.post(
|
||||
`${config.API_URL}/${elementName}Set`,
|
||||
frenchData,
|
||||
volunteer,
|
||||
axiosConfig
|
||||
)
|
||||
if (!data) {
|
||||
return { data }
|
||||
}
|
||||
|
||||
const englishData = _.mapValues(
|
||||
translation,
|
||||
(frenchProp: string) => data[frenchProp]
|
||||
) as Element
|
||||
return { data: englishData }
|
||||
return { data }
|
||||
} catch (error) {
|
||||
return { error: error as Error }
|
||||
}
|
||||
@@ -168,5 +120,30 @@ export default function getServiceAccessors<
|
||||
}
|
||||
}
|
||||
|
||||
return { listGet, get, set, add, countGet }
|
||||
function customPost(apiName: string): (params: any) => Promise<{
|
||||
data?: Element
|
||||
error?: Error
|
||||
}> {
|
||||
interface ElementGetResponse {
|
||||
data?: Element
|
||||
error?: Error
|
||||
}
|
||||
return async (params: any): Promise<ElementGetResponse> => {
|
||||
try {
|
||||
const { data } = await axios.post(
|
||||
`${config.API_URL}/${elementName}${apiName}`,
|
||||
params,
|
||||
axiosConfig
|
||||
)
|
||||
if (data.error) {
|
||||
throw Error(data.error)
|
||||
}
|
||||
return { data }
|
||||
} catch (error) {
|
||||
return { error: error as Error }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { listGet, get, set, add, countGet, customPost }
|
||||
}
|
||||
|
@@ -2,15 +2,15 @@ import { AxiosRequestConfig } from "axios"
|
||||
|
||||
const storage: any = localStorage
|
||||
|
||||
export const axiosConfig: AxiosRequestConfig = {
|
||||
headers: {},
|
||||
}
|
||||
|
||||
const jwt: string | null = storage?.getItem("id_token")
|
||||
if (jwt) {
|
||||
setJWT(jwt)
|
||||
}
|
||||
|
||||
export const axiosConfig: AxiosRequestConfig = {
|
||||
headers: {},
|
||||
}
|
||||
|
||||
export function setJWT(token: string): void {
|
||||
axiosConfig.headers.Authorization = `Bearer ${token}`
|
||||
storage?.setItem("id_token", token)
|
||||
|
@@ -54,10 +54,7 @@ const elementName = "JavGame"
|
||||
|
||||
export type JavGameWithoutId = Omit<JavGame, "id">
|
||||
|
||||
const { listGet, get, set, add } = getServiceAccessors<JavGameWithoutId, JavGame>(
|
||||
elementName,
|
||||
translationJavGame
|
||||
)
|
||||
const { listGet, get, set, add } = getServiceAccessors<JavGameWithoutId, JavGame>(elementName)
|
||||
|
||||
export const javGameListGet = listGet()
|
||||
export const javGameGet = get()
|
||||
|
@@ -28,12 +28,16 @@ export const translationPreVolunteer: { [k in keyof PreVolunteer]: string } = {
|
||||
|
||||
const elementName = "PreVolunteer"
|
||||
|
||||
export const emailRegexp =
|
||||
/^(([^<>()[\].,;:\s@"]+(\.[^<>()[\].,;:\s@"]+)*)|(".+"))@(([^<>()[\].,;:\s@"]+\.)+[^<>()[\].,;:\s@"]{2,})$/i
|
||||
export const passwordMinLength = 4
|
||||
|
||||
export type PreVolunteerWithoutId = Omit<PreVolunteer, "id">
|
||||
|
||||
const { listGet, get, set, add, countGet } = getServiceAccessors<
|
||||
PreVolunteerWithoutId,
|
||||
PreVolunteer
|
||||
>(elementName, translationPreVolunteer)
|
||||
>(elementName)
|
||||
|
||||
export const preVolunteerListGet = listGet()
|
||||
export const preVolunteerGet = get()
|
||||
|
@@ -23,7 +23,7 @@ export class Volunteer {
|
||||
|
||||
comment = ""
|
||||
|
||||
timestamp = ""
|
||||
timestamp = new Date()
|
||||
|
||||
password = ""
|
||||
}
|
||||
@@ -50,22 +50,18 @@ export const emailRegexp =
|
||||
/^(([^<>()[\].,;:\s@"]+(\.[^<>()[\].,;:\s@"]+)*)|(".+"))@(([^<>()[\].,;:\s@"]+\.)+[^<>()[\].,;:\s@"]{2,})$/i
|
||||
export const passwordMinLength = 4
|
||||
|
||||
export interface VolunteerLogin {
|
||||
volunteer?: {
|
||||
firstname: string
|
||||
}
|
||||
jwt?: string
|
||||
error?: string
|
||||
}
|
||||
|
||||
export type VolunteerWithoutId = Omit<Volunteer, "id">
|
||||
|
||||
const { listGet, get, set, add } = getServiceAccessors<VolunteerWithoutId, Volunteer>(
|
||||
elementName,
|
||||
translationVolunteer
|
||||
)
|
||||
const accessors = getServiceAccessors<VolunteerWithoutId, Volunteer>(elementName)
|
||||
const { listGet, get, set, add } = accessors
|
||||
|
||||
export const volunteerListGet = listGet()
|
||||
export const volunteerGet = get()
|
||||
export const volunteerAdd = add()
|
||||
export const volunteerSet = set()
|
||||
|
||||
export interface VolunteerLogin {
|
||||
firstname: string
|
||||
jwt: string
|
||||
}
|
||||
export const volunteerLogin = accessors.customPost("Login")
|
||||
|
@@ -27,10 +27,7 @@ const elementName = "Wish"
|
||||
|
||||
export type WishWithoutId = Omit<Wish, "id">
|
||||
|
||||
const { listGet, get, set, add } = getServiceAccessors<WishWithoutId, Wish>(
|
||||
elementName,
|
||||
translationWish
|
||||
)
|
||||
const { listGet, get, set, add } = getServiceAccessors<WishWithoutId, Wish>(elementName)
|
||||
|
||||
export const wishListGet = listGet()
|
||||
export const wishGet = get()
|
||||
|
Reference in New Issue
Block a user