client.ts
This commit is contained in:
parent
7717225411
commit
d333f6c97d
34
examples/user-authentication/frontend/src/client.ts
Normal file
34
examples/user-authentication/frontend/src/client.ts
Normal file
@ -0,0 +1,34 @@
|
||||
import { HttpClient } from "tsrpc-browser";
|
||||
import { setStatus } from "./index";
|
||||
import { BaseResponse } from "./shared/protocols/base";
|
||||
import { serviceProto } from "./shared/protocols/serviceProto";
|
||||
|
||||
// Create Client
|
||||
export const client = new HttpClient(serviceProto, {
|
||||
server: 'http://127.0.0.1:3000',
|
||||
logger: console
|
||||
});
|
||||
|
||||
// When server return a SSOToken, store it to localStorage
|
||||
client.flows.postApiReturnFlow.push(v => {
|
||||
if (v.return.isSucc) {
|
||||
let res = v.return.res as BaseResponse;
|
||||
if (res.__ssoToken !== undefined) {
|
||||
localStorage.setItem('SSO_TOKEN', res.__ssoToken);
|
||||
}
|
||||
}
|
||||
else if (v.return.err.code === 'NEED_LOGIN') {
|
||||
localStorage.removeItem('SSO_TOKEN');
|
||||
setStatus(false);
|
||||
}
|
||||
return v;
|
||||
});
|
||||
|
||||
// Append "__ssoToken" to request automatically
|
||||
client.flows.preCallApiFlow.push(v => {
|
||||
let ssoToken = localStorage.getItem('SSO_TOKEN');
|
||||
if (ssoToken) {
|
||||
v.req.__ssoToken = ssoToken;
|
||||
}
|
||||
return v;
|
||||
})
|
@ -1,38 +1,7 @@
|
||||
import { HttpClient } from 'tsrpc-browser';
|
||||
import { BaseResponse } from './shared/protocols/base';
|
||||
import { serviceProto } from './shared/protocols/serviceProto';
|
||||
import { client } from "./client";
|
||||
|
||||
const $ = document.querySelector.bind(document) as (v: string) => HTMLElement;
|
||||
|
||||
// Create Client
|
||||
let client = new HttpClient(serviceProto, {
|
||||
server: 'http://127.0.0.1:3000',
|
||||
logger: console
|
||||
});
|
||||
|
||||
// When server return a SSOToken, store it to localStorage
|
||||
client.flows.postApiReturnFlow.push(v => {
|
||||
if (v.return.isSucc) {
|
||||
let res = v.return.res as BaseResponse;
|
||||
if (res.__ssoToken !== undefined) {
|
||||
localStorage.setItem('SSO_TOKEN', res.__ssoToken);
|
||||
}
|
||||
}
|
||||
else if (v.return.err.code === 'NEED_LOGIN') {
|
||||
localStorage.removeItem('SSO_TOKEN');
|
||||
setStatus(false);
|
||||
}
|
||||
return v;
|
||||
});
|
||||
// Append "__ssoToken" to request automatically
|
||||
client.flows.preCallApiFlow.push(v => {
|
||||
let ssoToken = localStorage.getItem('SSO_TOKEN');
|
||||
if (ssoToken) {
|
||||
v.req.__ssoToken = ssoToken;
|
||||
}
|
||||
return v;
|
||||
})
|
||||
|
||||
// Login & Logout
|
||||
$('.login-normal button').onclick = async () => {
|
||||
let ret = await client.callApi('user/Login', {
|
||||
@ -96,7 +65,7 @@ $('.action .admin button').onclick = async () => {
|
||||
$('.action .admin .return').style.display = 'block';
|
||||
}
|
||||
|
||||
function setStatus(logined: boolean) {
|
||||
export function setStatus(logined: boolean) {
|
||||
if (logined) {
|
||||
$('.status').className = 'status logined';
|
||||
$('.status').innerText = `Logined as ${localStorage.getItem('LoginedRole')} Role`;
|
||||
@ -107,4 +76,4 @@ function setStatus(logined: boolean) {
|
||||
}
|
||||
}
|
||||
// Init logined status
|
||||
setStatus(!!localStorage.getItem('SSO_TOKEN'));
|
||||
setStatus(!!localStorage.getItem('SSO_TOKEN'));
|
||||
|
Loading…
Reference in New Issue
Block a user