add url to login api

This commit is contained in:
Jesper Møller Jensen
2023-05-24 19:36:23 +12:00
parent a805cd693a
commit 0bdd304b95
6 changed files with 39 additions and 47 deletions

View File

@@ -1,11 +1,7 @@
import { LoginRequestModel, UmbAuthContext } from './types';
import { LoginRequestModel, IUmbAuthContext } from './types';
export class UmbAuthLegacyContext implements UmbAuthContext {
authUrl: string;
constructor(authUrl: string) {
this.authUrl = authUrl;
}
export class UmbAuthLegacyContext implements IUmbAuthContext {
readonly #AUTH_URL = '/umbraco/backoffice/umbracoapi/authentication/postlogin';
login(data: LoginRequestModel): Promise<{ error?: string | undefined }> {
throw new Error('Method not implemented.');

View File

@@ -1,26 +0,0 @@
import { LoginRequestModel, UmbAuthContext } from './types';
export class UmbAuthNewContext implements UmbAuthContext {
authUrl: string;
constructor(authUrl: string) {
this.authUrl = authUrl;
}
async login(data: LoginRequestModel) {
//TODO: call authUrl with data
const { error } = await UmbMockAPI.login(data, false);
//TODO Should the redirect be done here? or in the login element?
return { error };
}
}
class UmbMockAPI {
static async login(data: LoginRequestModel, shouldFail = false) {
await new Promise((resolve) => setTimeout(resolve, 1000));
return shouldFail ? { error: 'Invalid credentials' } : {};
}
}

View File

@@ -0,0 +1,24 @@
import { LoginRequestModel, IUmbAuthContext } from './types';
export class UmbAuthContext implements IUmbAuthContext {
readonly #AUTH_URL = '/umbraco/management/api/v1.0/security/back-office';
async login(data: LoginRequestModel) {
//TODO: call authUrl with data
return UmbMockAPI.loginSuccess(data);
}
}
class UmbMockAPI {
static async loginSuccess(data: LoginRequestModel) {
await new Promise((resolve) => setTimeout(resolve, 1000));
return {};
}
static async loginFail(data: LoginRequestModel) {
await new Promise((resolve) => setTimeout(resolve, 1000));
return { error: 'Invalid credentials' };
}
}

View File

@@ -1,4 +0,0 @@
export default function() {
sessionStorage.setItem('is-authenticated', 'true');
history.replaceState(null, '', 'section');
}

View File

@@ -3,22 +3,19 @@ import { css, CSSResultGroup, html, LitElement } from 'lit';
import { customElement, property, state } from 'lit/decorators.js';
import { UUIButtonState } from '@umbraco-ui/uui';
import { UmbAuthContext } from './types';
import { IUmbAuthContext } from './types';
import { UmbAuthLegacyContext } from './auth-legacy.context';
import { UmbAuthNewContext } from './auth-new.context';
import { UmbAuthContext } from './auth.context';
import './auth-layout.element';
@customElement('umb-login')
export default class UmbLoginElement extends LitElement {
#authContext: UmbAuthContext;
#authContext: IUmbAuthContext;
@property({ type: String, attribute: 'return-url' })
returnUrl = '';
@property({ type: String, attribute: 'auth-url' })
authUrl = '';
@property({ type: Boolean })
isLegacy = false;
@@ -34,9 +31,9 @@ export default class UmbLoginElement extends LitElement {
constructor() {
super();
if (this.isLegacy) {
this.#authContext = new UmbAuthLegacyContext(this.authUrl);
this.#authContext = new UmbAuthLegacyContext();
} else {
this.#authContext = new UmbAuthNewContext(this.authUrl);
this.#authContext = new UmbAuthContext();
}
}

View File

@@ -4,7 +4,12 @@ export type LoginRequestModel = {
persist: boolean;
};
export interface UmbAuthContext {
authUrl: string;
export interface IUmbAuthContext {
login(data: LoginRequestModel): Promise<{ error?: string }>;
}
export type ProblemDetails = {
type: string;
title: string;
message: string;
};