ajout app
This commit is contained in:
58
Voltaserve/ui/src/client/idp/account.ts
Normal file
58
Voltaserve/ui/src/client/idp/account.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { idpFetcher } from '@/client/fetcher'
|
||||
import { User } from './user'
|
||||
|
||||
export type CreateOptions = {
|
||||
email: string
|
||||
password: string
|
||||
fullName: string
|
||||
picture?: string
|
||||
}
|
||||
|
||||
export type SendResetPasswordEmailOptions = {
|
||||
email: string
|
||||
}
|
||||
|
||||
export type ResetPasswordOptions = {
|
||||
token: string
|
||||
newPassword: string
|
||||
}
|
||||
|
||||
export type ConfirmEmailOptions = {
|
||||
token: string
|
||||
}
|
||||
|
||||
export default class AccountAPI {
|
||||
static async create(options: CreateOptions) {
|
||||
return idpFetcher({
|
||||
url: `/accounts`,
|
||||
method: 'POST',
|
||||
body: JSON.stringify(options),
|
||||
redirect: false,
|
||||
authenticate: false,
|
||||
}) as Promise<User>
|
||||
}
|
||||
|
||||
static async sendResetPasswordEmail(options: SendResetPasswordEmailOptions) {
|
||||
return idpFetcher({
|
||||
url: `/accounts/send_reset_password_email`,
|
||||
method: 'POST',
|
||||
body: JSON.stringify(options),
|
||||
})
|
||||
}
|
||||
|
||||
static async resetPassword(options: ResetPasswordOptions) {
|
||||
return idpFetcher({
|
||||
url: `/accounts/reset_password`,
|
||||
method: 'POST',
|
||||
body: JSON.stringify(options),
|
||||
})
|
||||
}
|
||||
|
||||
static async confirmEmail(options: ConfirmEmailOptions) {
|
||||
return idpFetcher({
|
||||
url: `/accounts/confirm_email`,
|
||||
method: 'POST',
|
||||
body: JSON.stringify(options),
|
||||
})
|
||||
}
|
||||
}
|
||||
66
Voltaserve/ui/src/client/idp/token.ts
Normal file
66
Voltaserve/ui/src/client/idp/token.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import { baseFetcher } from '@/client/fetcher'
|
||||
import { getConfig } from '@/config/config'
|
||||
|
||||
export type GrantType = 'password' | 'refresh_token'
|
||||
|
||||
export type Token = {
|
||||
access_token: string
|
||||
expires_in: number
|
||||
token_type: string
|
||||
refresh_token: string
|
||||
}
|
||||
|
||||
export type ExchangeOptions = {
|
||||
grant_type: GrantType
|
||||
username?: string
|
||||
password?: string
|
||||
refresh_token?: string
|
||||
locale?: string
|
||||
}
|
||||
|
||||
export default class TokenAPI {
|
||||
static async exchange(options: ExchangeOptions) {
|
||||
const formBody = []
|
||||
formBody.push(`grant_type=${options.grant_type}`)
|
||||
if (options.grant_type === 'password') {
|
||||
if (options.username && options.password) {
|
||||
formBody.push(`username=${encodeURIComponent(options.username)}`)
|
||||
formBody.push(`password=${encodeURIComponent(options.password)}`)
|
||||
} else {
|
||||
throw new Error('Username or password missing!')
|
||||
}
|
||||
}
|
||||
if (options.grant_type === 'refresh_token') {
|
||||
if (options.refresh_token) {
|
||||
formBody.push(
|
||||
`refresh_token=${encodeURIComponent(options.refresh_token)}`,
|
||||
)
|
||||
} else {
|
||||
throw new Error('Refresh token missing!')
|
||||
}
|
||||
}
|
||||
if (options.locale) {
|
||||
formBody.push(`&locale=${encodeURIComponent(options.locale)}`)
|
||||
}
|
||||
const response = await baseFetcher(
|
||||
`${getConfig().idpURL}/token`,
|
||||
{
|
||||
method: 'POST',
|
||||
body: formBody.join('&'),
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
},
|
||||
false,
|
||||
)
|
||||
try {
|
||||
if (response) {
|
||||
return (await response.json()) as Token
|
||||
} else {
|
||||
throw new Error('No response')
|
||||
}
|
||||
} catch {
|
||||
throw new Error('Failed to parse token')
|
||||
}
|
||||
}
|
||||
}
|
||||
104
Voltaserve/ui/src/client/idp/user.ts
Normal file
104
Voltaserve/ui/src/client/idp/user.ts
Normal file
@@ -0,0 +1,104 @@
|
||||
/* eslint-disable react-hooks/rules-of-hooks */
|
||||
import useSWR from 'swr'
|
||||
import { idpFetcher } from '@/client/fetcher'
|
||||
|
||||
export type User = {
|
||||
id: string
|
||||
username: string
|
||||
email: string
|
||||
fullName: string
|
||||
picture?: string
|
||||
pendingEmail?: string
|
||||
}
|
||||
|
||||
export type UpdateFullNameOptions = {
|
||||
fullName: string
|
||||
}
|
||||
|
||||
export type UpdateEmailRequestOptions = {
|
||||
email: string
|
||||
}
|
||||
|
||||
export type UpdateEmailConfirmationOptions = {
|
||||
token: string
|
||||
}
|
||||
|
||||
export type UpdatePasswordOptions = {
|
||||
currentPassword: string
|
||||
newPassword: string
|
||||
}
|
||||
|
||||
export type DeleteOptions = {
|
||||
password: string
|
||||
}
|
||||
|
||||
export default class UserAPI {
|
||||
static useGet(swrOptions?: any) {
|
||||
const url = `/user`
|
||||
return useSWR<User>(
|
||||
url,
|
||||
() => idpFetcher({ url, method: 'GET' }) as Promise<User>,
|
||||
swrOptions,
|
||||
)
|
||||
}
|
||||
|
||||
static async updateFullName(options: UpdateFullNameOptions) {
|
||||
return idpFetcher({
|
||||
url: `/user/update_full_name`,
|
||||
method: 'POST',
|
||||
body: JSON.stringify(options),
|
||||
}) as Promise<User>
|
||||
}
|
||||
|
||||
static async updateEmailRequest(options: UpdateEmailRequestOptions) {
|
||||
return idpFetcher({
|
||||
url: `/user/update_email_request`,
|
||||
method: 'POST',
|
||||
body: JSON.stringify(options),
|
||||
}) as Promise<User>
|
||||
}
|
||||
|
||||
static async updateEmailConfirmation(
|
||||
options: UpdateEmailConfirmationOptions,
|
||||
) {
|
||||
return idpFetcher({
|
||||
url: `/user/update_email_confirmation`,
|
||||
method: 'POST',
|
||||
body: JSON.stringify(options),
|
||||
}) as Promise<User>
|
||||
}
|
||||
|
||||
static async updatePassword(options: UpdatePasswordOptions) {
|
||||
return idpFetcher({
|
||||
url: `/user/update_password`,
|
||||
method: 'POST',
|
||||
body: JSON.stringify(options),
|
||||
}) as Promise<User>
|
||||
}
|
||||
|
||||
static async delete(options: DeleteOptions) {
|
||||
return idpFetcher({
|
||||
url: `/user`,
|
||||
method: 'DELETE',
|
||||
body: JSON.stringify(options),
|
||||
})
|
||||
}
|
||||
|
||||
static async updatePicture(file: File) {
|
||||
const body = new FormData()
|
||||
body.append('file', file)
|
||||
return idpFetcher({
|
||||
url: `/user/update_picture`,
|
||||
method: 'POST',
|
||||
body,
|
||||
contentType: 'multipart/form-data',
|
||||
}) as Promise<User>
|
||||
}
|
||||
|
||||
static async deletePicture() {
|
||||
return idpFetcher({
|
||||
url: `/user/delete_picture`,
|
||||
method: 'POST',
|
||||
}) as Promise<User>
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user