mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-20 10:15:17 -04:00
Adapt react-client to use the real backend API (#1545)
Co-authored-by: Philip Molares <philip.molares@udo.edu> Co-authored-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
parent
3399ed2023
commit
26f90505ff
227 changed files with 4726 additions and 2310 deletions
|
@ -1,34 +1,14 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
||||
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
import { defaultFetchConfig, expectResponseCode, getApiUrl } from '../utils'
|
||||
|
||||
export const INTERACTIVE_LOGIN_METHODS = ['local', 'ldap']
|
||||
|
||||
export enum AuthError {
|
||||
INVALID_CREDENTIALS = 'invalidCredentials',
|
||||
LOGIN_DISABLED = 'loginDisabled',
|
||||
OPENID_ERROR = 'openIdError',
|
||||
OTHER = 'other'
|
||||
}
|
||||
|
||||
export enum RegisterError {
|
||||
USERNAME_EXISTING = 'usernameExisting',
|
||||
REGISTRATION_DISABLED = 'registrationDisabled',
|
||||
OTHER = 'other'
|
||||
}
|
||||
import { DeleteApiRequestBuilder } from '../common/api-request-builder/delete-api-request-builder'
|
||||
|
||||
/**
|
||||
* Requests to logout the current user.
|
||||
* Requests to log out the current user.
|
||||
* @throws Error if logout is not possible.
|
||||
*/
|
||||
export const doLogout = async (): Promise<void> => {
|
||||
const response = await fetch(getApiUrl() + 'auth/logout', {
|
||||
...defaultFetchConfig,
|
||||
method: 'DELETE'
|
||||
})
|
||||
|
||||
expectResponseCode(response)
|
||||
await new DeleteApiRequestBuilder('auth/logout').sendRequest()
|
||||
}
|
||||
|
|
|
@ -1,25 +1,28 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
||||
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { defaultFetchConfig, expectResponseCode, getApiUrl } from '../utils'
|
||||
import type { LoginDto } from './types'
|
||||
import { AuthError } from './types'
|
||||
import { PostApiRequestBuilder } from '../common/api-request-builder/post-api-request-builder'
|
||||
|
||||
/**
|
||||
* Requests to login a user via LDAP credentials.
|
||||
* @param provider The identifier of the LDAP provider with which to login.
|
||||
* @param username The username with which to try the login.
|
||||
* @param password The password of the user.
|
||||
* @throws {AuthError.INVALID_CREDENTIALS} if the LDAP provider denied the given credentials.
|
||||
*/
|
||||
export const doLdapLogin = async (username: string, password: string): Promise<void> => {
|
||||
const response = await fetch(getApiUrl() + 'auth/ldap', {
|
||||
...defaultFetchConfig,
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
export const doLdapLogin = async (provider: string, username: string, password: string): Promise<void> => {
|
||||
await new PostApiRequestBuilder<void, LoginDto>('auth/ldap/' + provider)
|
||||
.withJsonBody({
|
||||
username: username,
|
||||
password: password
|
||||
})
|
||||
})
|
||||
|
||||
expectResponseCode(response)
|
||||
.withStatusCodeErrorMapping({
|
||||
401: AuthError.INVALID_CREDENTIALS
|
||||
})
|
||||
.sendRequest()
|
||||
}
|
||||
|
|
|
@ -1,38 +1,31 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
||||
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { defaultFetchConfig, expectResponseCode, getApiUrl } from '../utils'
|
||||
import { AuthError, RegisterError } from './index'
|
||||
import type { ChangePasswordDto, LoginDto, RegisterDto } from './types'
|
||||
import { AuthError, RegisterError } from './types'
|
||||
import { PostApiRequestBuilder } from '../common/api-request-builder/post-api-request-builder'
|
||||
import { PutApiRequestBuilder } from '../common/api-request-builder/put-api-request-builder'
|
||||
|
||||
/**
|
||||
* Requests to do a local login with a provided username and password.
|
||||
* @param username The username for which the login should be tried.
|
||||
* @param password The password which should be used to login.
|
||||
* @param password The password which should be used to log in.
|
||||
* @throws {AuthError.INVALID_CREDENTIALS} when the username or password is wrong.
|
||||
* @throws {AuthError.LOGIN_DISABLED} when the local login is disabled on the backend.
|
||||
*/
|
||||
export const doLocalLogin = async (username: string, password: string): Promise<void> => {
|
||||
const response = await fetch(getApiUrl() + 'auth/local/login', {
|
||||
...defaultFetchConfig,
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
await new PostApiRequestBuilder<void, LoginDto>('auth/local/login')
|
||||
.withJsonBody({
|
||||
username,
|
||||
password
|
||||
})
|
||||
})
|
||||
|
||||
if (response.status === 400) {
|
||||
throw new Error(AuthError.LOGIN_DISABLED)
|
||||
}
|
||||
|
||||
if (response.status === 401) {
|
||||
throw new Error(AuthError.INVALID_CREDENTIALS)
|
||||
}
|
||||
|
||||
expectResponseCode(response, 201)
|
||||
.withStatusCodeErrorMapping({
|
||||
400: AuthError.LOGIN_DISABLED,
|
||||
401: AuthError.INVALID_CREDENTIALS
|
||||
})
|
||||
.sendRequest()
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -40,29 +33,21 @@ export const doLocalLogin = async (username: string, password: string): Promise<
|
|||
* @param username The username of the new user.
|
||||
* @param displayName The display name of the new user.
|
||||
* @param password The password of the new user.
|
||||
* @throws {RegisterError.USERNAME_EXISTING} when there is already an existing user with the same user name.
|
||||
* @throws {RegisterError.USERNAME_EXISTING} when there is already an existing user with the same username.
|
||||
* @throws {RegisterError.REGISTRATION_DISABLED} when the registration of local users has been disabled on the backend.
|
||||
*/
|
||||
export const doLocalRegister = async (username: string, displayName: string, password: string): Promise<void> => {
|
||||
const response = await fetch(getApiUrl() + 'auth/local', {
|
||||
...defaultFetchConfig,
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
await new PostApiRequestBuilder<void, RegisterDto>('auth/local')
|
||||
.withJsonBody({
|
||||
username,
|
||||
displayName,
|
||||
password
|
||||
})
|
||||
})
|
||||
|
||||
if (response.status === 409) {
|
||||
throw new Error(RegisterError.USERNAME_EXISTING)
|
||||
}
|
||||
|
||||
if (response.status === 400) {
|
||||
throw new Error(RegisterError.REGISTRATION_DISABLED)
|
||||
}
|
||||
|
||||
expectResponseCode(response)
|
||||
.withStatusCodeErrorMapping({
|
||||
400: RegisterError.REGISTRATION_DISABLED,
|
||||
409: RegisterError.USERNAME_EXISTING
|
||||
})
|
||||
.sendRequest()
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -73,22 +58,14 @@ export const doLocalRegister = async (username: string, displayName: string, pas
|
|||
* @throws {AuthError.LOGIN_DISABLED} when local login is disabled on the backend.
|
||||
*/
|
||||
export const doLocalPasswordChange = async (currentPassword: string, newPassword: string): Promise<void> => {
|
||||
const response = await fetch(getApiUrl() + 'auth/local', {
|
||||
...defaultFetchConfig,
|
||||
method: 'PUT',
|
||||
body: JSON.stringify({
|
||||
await new PutApiRequestBuilder<void, ChangePasswordDto>('auth/local')
|
||||
.withJsonBody({
|
||||
currentPassword,
|
||||
newPassword
|
||||
})
|
||||
})
|
||||
|
||||
if (response.status === 401) {
|
||||
throw new Error(AuthError.INVALID_CREDENTIALS)
|
||||
}
|
||||
|
||||
if (response.status === 400) {
|
||||
throw new Error(AuthError.LOGIN_DISABLED)
|
||||
}
|
||||
|
||||
expectResponseCode(response)
|
||||
.withStatusCodeErrorMapping({
|
||||
400: AuthError.LOGIN_DISABLED,
|
||||
401: AuthError.INVALID_CREDENTIALS
|
||||
})
|
||||
.sendRequest()
|
||||
}
|
||||
|
|
33
src/api/auth/types.ts
Normal file
33
src/api/auth/types.ts
Normal file
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
export enum AuthError {
|
||||
INVALID_CREDENTIALS = 'invalidCredentials',
|
||||
LOGIN_DISABLED = 'loginDisabled',
|
||||
OPENID_ERROR = 'openIdError',
|
||||
OTHER = 'other'
|
||||
}
|
||||
|
||||
export enum RegisterError {
|
||||
USERNAME_EXISTING = 'usernameExisting',
|
||||
REGISTRATION_DISABLED = 'registrationDisabled',
|
||||
OTHER = 'other'
|
||||
}
|
||||
|
||||
export interface LoginDto {
|
||||
username: string
|
||||
password: string
|
||||
}
|
||||
|
||||
export interface RegisterDto {
|
||||
username: string
|
||||
password: string
|
||||
displayName: string
|
||||
}
|
||||
|
||||
export interface ChangePasswordDto {
|
||||
currentPassword: string
|
||||
newPassword: string
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue