add repository
This commit is contained in:
@@ -1,32 +1,12 @@
|
||||
import { UmbAuthRepository } from './auth.repository';
|
||||
import { LoginRequestModel, IUmbAuthContext } from './types';
|
||||
|
||||
export class UmbAuthContext implements IUmbAuthContext {
|
||||
readonly #AUTH_URL = '/umbraco/management/api/v1/security/back-office';
|
||||
|
||||
getErrorText(response: Response) {
|
||||
switch (response.status) {
|
||||
case 401:
|
||||
return 'Oops! It seems like your login credentials are invalid or expired. Please double-check your username and password and try again.';
|
||||
#authRepository = new UmbAuthRepository(this.#AUTH_URL);
|
||||
|
||||
default:
|
||||
return response.statusText;
|
||||
}
|
||||
}
|
||||
|
||||
async login(data: LoginRequestModel) {
|
||||
//TODO: call authUrl with data
|
||||
const request = new Request(this.#AUTH_URL + '/login', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
username: data.username,
|
||||
password: data.password,
|
||||
}),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
const response = await fetch(request);
|
||||
|
||||
return { error: response.ok ? undefined : this.getErrorText(response) };
|
||||
public async login(data: LoginRequestModel) {
|
||||
return this.#authRepository.login(data);
|
||||
}
|
||||
}
|
||||
|
||||
44
src/Umbraco.Web.UI.Client/apps/auth/src/auth.repository.ts
Normal file
44
src/Umbraco.Web.UI.Client/apps/auth/src/auth.repository.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { LoginRequestModel } from './types';
|
||||
|
||||
export class UmbAuthRepository {
|
||||
#authURL = '';
|
||||
|
||||
constructor(authUrl: string) {
|
||||
this.#authURL = authUrl;
|
||||
}
|
||||
|
||||
public async login(data: LoginRequestModel) {
|
||||
const request = new Request(this.#authURL + '/login', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
username: data.username,
|
||||
password: data.password,
|
||||
}),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
const response = await fetch(request);
|
||||
|
||||
//TODO: What kind of data does the old backoffice expect?
|
||||
//NOTE: this conditionally adds error and data to the response object
|
||||
return {
|
||||
status: response.status,
|
||||
...(!response.ok && { error: this.#getErrorText(response) }),
|
||||
...(response.ok && { data: 'WHAT DATA SHOULD BE RETURNED?' }),
|
||||
};
|
||||
}
|
||||
|
||||
#getErrorText(response: Response) {
|
||||
switch (response.status) {
|
||||
case 401:
|
||||
return 'Oops! It seems like your login credentials are invalid or expired. Please double-check your username and password and try again.';
|
||||
|
||||
case 500:
|
||||
return "We're sorry, but the server encountered an unexpected error. Please refresh the page or try again later..";
|
||||
|
||||
default:
|
||||
return response.statusText;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,11 +5,11 @@ export type LoginRequestModel = {
|
||||
};
|
||||
|
||||
export interface IUmbAuthContext {
|
||||
login(data: LoginRequestModel): Promise<{ error?: string }>;
|
||||
login(data: LoginRequestModel): Promise<LoginResponse>;
|
||||
}
|
||||
|
||||
export type ProblemDetails = {
|
||||
type: string;
|
||||
title: string;
|
||||
message: string;
|
||||
export type LoginResponse = {
|
||||
data?: string;
|
||||
error?: string;
|
||||
status: number;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user