user-authentication
This commit is contained in:
108
examples/user-authentication/frontend/src/index.ts
Normal file
108
examples/user-authentication/frontend/src/index.ts
Normal file
@@ -0,0 +1,108 @@
|
||||
import { HttpClient } from 'tsrpc-browser';
|
||||
import { BaseResponse } from './shared/protocols/base';
|
||||
import { serviceProto } from './shared/protocols/serviceProto';
|
||||
|
||||
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
|
||||
});
|
||||
|
||||
// Flow
|
||||
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;
|
||||
});
|
||||
client.flows.preCallApiFlow.push(v => {
|
||||
let ssoToken = localStorage.getItem('SSO_TOKEN');
|
||||
if (ssoToken) {
|
||||
v.req.__ssoToken = ssoToken;
|
||||
}
|
||||
return v;
|
||||
})
|
||||
|
||||
// User
|
||||
$('.login-normal button').onclick = async () => {
|
||||
let ret = await client.callApi('user/Login', {
|
||||
username: 'Normal',
|
||||
password: '123456'
|
||||
});
|
||||
|
||||
if (!ret.isSucc) {
|
||||
alert(ret.err.message);
|
||||
return;
|
||||
}
|
||||
|
||||
localStorage.setItem('LoginedRole', 'Normal');
|
||||
setStatus(true);
|
||||
document.querySelectorAll<HTMLElement>('.return').forEach(v => { v.style.display = 'none' });
|
||||
}
|
||||
$('.login-admin button').onclick = async () => {
|
||||
let ret = await client.callApi('user/Login', {
|
||||
username: 'Admin',
|
||||
password: '123456'
|
||||
});
|
||||
|
||||
if (!ret.isSucc) {
|
||||
alert(ret.err.message);
|
||||
return;
|
||||
}
|
||||
|
||||
localStorage.setItem('LoginedRole', 'Admin');
|
||||
setStatus(true);
|
||||
document.querySelectorAll<HTMLElement>('.return').forEach(v => { v.style.display = 'none' });
|
||||
}
|
||||
$('.logout button').onclick = async () => {
|
||||
let ret = await client.callApi('user/Logout', {});
|
||||
|
||||
if (!ret.isSucc) {
|
||||
alert(ret.err.message);
|
||||
return;
|
||||
}
|
||||
|
||||
setStatus(false);
|
||||
document.querySelectorAll<HTMLElement>('.return').forEach(v => { v.style.display = 'none' });
|
||||
}
|
||||
|
||||
// Actions
|
||||
$('.action .guest button').onclick = async () => {
|
||||
let ret = await client.callApi('action/GuestAction', {});
|
||||
$('.action .guest pre').innerText = JSON.stringify(ret, null, 2);
|
||||
$('.action .guest pre').style.background = ret.isSucc ? 'green' : 'darkred';
|
||||
$('.action .guest .return').style.display = 'block';
|
||||
}
|
||||
$('.action .normal button').onclick = async () => {
|
||||
let ret = await client.callApi('action/NormalAction', {});
|
||||
$('.action .normal pre').innerText = JSON.stringify(ret, null, 2);
|
||||
$('.action .normal pre').style.background = ret.isSucc ? 'green' : 'darkred';
|
||||
$('.action .normal .return').style.display = 'block';
|
||||
}
|
||||
$('.action .admin button').onclick = async () => {
|
||||
let ret = await client.callApi('action/AdminAction', {});
|
||||
$('.action .admin pre').innerText = JSON.stringify(ret, null, 2);
|
||||
$('.action .admin pre').style.background = ret.isSucc ? 'green' : 'darkred';
|
||||
$('.action .admin .return').style.display = 'block';
|
||||
}
|
||||
|
||||
function setStatus(logined: boolean) {
|
||||
if (logined) {
|
||||
$('.status').className = 'status logined';
|
||||
$('.status').innerText = `Logined as ${localStorage.getItem('LoginedRole')} Role`;
|
||||
}
|
||||
else {
|
||||
$('.status').className = 'status';
|
||||
$('.status').innerText = 'Not Logined';
|
||||
}
|
||||
}
|
||||
setStatus(!!localStorage.getItem('SSO_TOKEN'));
|
@@ -0,0 +1,14 @@
|
||||
import { BaseRequest, BaseResponse, BaseConf } from '../base'
|
||||
|
||||
export interface ReqAdminAction extends BaseRequest {
|
||||
|
||||
}
|
||||
|
||||
export interface ResAdminAction extends BaseResponse {
|
||||
result: string
|
||||
}
|
||||
|
||||
export const conf: BaseConf = {
|
||||
needLogin: true,
|
||||
needRoles: ['Admin']
|
||||
};
|
@@ -0,0 +1,13 @@
|
||||
import { BaseConf, BaseRequest, BaseResponse } from '../base';
|
||||
|
||||
export interface ReqGuestAction extends BaseRequest {
|
||||
|
||||
}
|
||||
|
||||
export interface ResGuestAction extends BaseResponse {
|
||||
result: string
|
||||
}
|
||||
|
||||
export const conf: BaseConf = {
|
||||
needLogin: false
|
||||
};
|
@@ -0,0 +1,13 @@
|
||||
import { BaseConf, BaseRequest, BaseResponse } from '../base';
|
||||
|
||||
export interface ReqNormalAction extends BaseRequest {
|
||||
|
||||
}
|
||||
|
||||
export interface ResNormalAction extends BaseResponse {
|
||||
result: string
|
||||
}
|
||||
|
||||
export const conf: BaseConf = {
|
||||
needLogin: true
|
||||
};
|
@@ -0,0 +1,13 @@
|
||||
export interface BaseRequest {
|
||||
__ssoToken?: string;
|
||||
}
|
||||
|
||||
export interface BaseResponse {
|
||||
// Init or refresh sso token
|
||||
__ssoToken?: string;
|
||||
}
|
||||
|
||||
export interface BaseConf {
|
||||
needLogin?: boolean,
|
||||
needRoles?: string[]
|
||||
}
|
@@ -0,0 +1,279 @@
|
||||
import { ServiceProto } from 'tsrpc-proto';
|
||||
import { ReqAdminAction, ResAdminAction } from './action/PtlAdminAction';
|
||||
import { ReqGuestAction, ResGuestAction } from './action/PtlGuestAction';
|
||||
import { ReqNormalAction, ResNormalAction } from './action/PtlNormalAction';
|
||||
import { ReqLogin, ResLogin } from './user/PtlLogin';
|
||||
import { ReqLogout, ResLogout } from './user/PtlLogout';
|
||||
|
||||
export interface ServiceType {
|
||||
api: {
|
||||
"action/AdminAction": {
|
||||
req: ReqAdminAction,
|
||||
res: ResAdminAction
|
||||
},
|
||||
"action/GuestAction": {
|
||||
req: ReqGuestAction,
|
||||
res: ResGuestAction
|
||||
},
|
||||
"action/NormalAction": {
|
||||
req: ReqNormalAction,
|
||||
res: ResNormalAction
|
||||
},
|
||||
"user/Login": {
|
||||
req: ReqLogin,
|
||||
res: ResLogin
|
||||
},
|
||||
"user/Logout": {
|
||||
req: ReqLogout,
|
||||
res: ResLogout
|
||||
}
|
||||
},
|
||||
msg: {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export const serviceProto: ServiceProto<ServiceType> = {
|
||||
"version": 2,
|
||||
"services": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "action/AdminAction",
|
||||
"type": "api",
|
||||
"conf": {
|
||||
"needLogin": true,
|
||||
"needRoles": [
|
||||
"Admin"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 1,
|
||||
"name": "action/GuestAction",
|
||||
"type": "api",
|
||||
"conf": {
|
||||
"needLogin": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"name": "action/NormalAction",
|
||||
"type": "api",
|
||||
"conf": {
|
||||
"needLogin": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
"name": "user/Login",
|
||||
"type": "api",
|
||||
"conf": {}
|
||||
},
|
||||
{
|
||||
"id": 4,
|
||||
"name": "user/Logout",
|
||||
"type": "api",
|
||||
"conf": {}
|
||||
}
|
||||
],
|
||||
"types": {
|
||||
"action/PtlAdminAction/ReqAdminAction": {
|
||||
"type": "Interface",
|
||||
"extends": [
|
||||
{
|
||||
"id": 0,
|
||||
"type": {
|
||||
"type": "Reference",
|
||||
"target": "base/BaseRequest"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"base/BaseRequest": {
|
||||
"type": "Interface",
|
||||
"properties": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "__ssoToken",
|
||||
"type": {
|
||||
"type": "String"
|
||||
},
|
||||
"optional": true
|
||||
}
|
||||
]
|
||||
},
|
||||
"action/PtlAdminAction/ResAdminAction": {
|
||||
"type": "Interface",
|
||||
"extends": [
|
||||
{
|
||||
"id": 0,
|
||||
"type": {
|
||||
"type": "Reference",
|
||||
"target": "base/BaseResponse"
|
||||
}
|
||||
}
|
||||
],
|
||||
"properties": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "result",
|
||||
"type": {
|
||||
"type": "String"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"base/BaseResponse": {
|
||||
"type": "Interface",
|
||||
"properties": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "__ssoToken",
|
||||
"type": {
|
||||
"type": "String"
|
||||
},
|
||||
"optional": true
|
||||
}
|
||||
]
|
||||
},
|
||||
"action/PtlGuestAction/ReqGuestAction": {
|
||||
"type": "Interface",
|
||||
"extends": [
|
||||
{
|
||||
"id": 0,
|
||||
"type": {
|
||||
"type": "Reference",
|
||||
"target": "base/BaseRequest"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"action/PtlGuestAction/ResGuestAction": {
|
||||
"type": "Interface",
|
||||
"extends": [
|
||||
{
|
||||
"id": 0,
|
||||
"type": {
|
||||
"type": "Reference",
|
||||
"target": "base/BaseResponse"
|
||||
}
|
||||
}
|
||||
],
|
||||
"properties": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "result",
|
||||
"type": {
|
||||
"type": "String"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"action/PtlNormalAction/ReqNormalAction": {
|
||||
"type": "Interface",
|
||||
"extends": [
|
||||
{
|
||||
"id": 0,
|
||||
"type": {
|
||||
"type": "Reference",
|
||||
"target": "base/BaseRequest"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"action/PtlNormalAction/ResNormalAction": {
|
||||
"type": "Interface",
|
||||
"extends": [
|
||||
{
|
||||
"id": 0,
|
||||
"type": {
|
||||
"type": "Reference",
|
||||
"target": "base/BaseResponse"
|
||||
}
|
||||
}
|
||||
],
|
||||
"properties": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "result",
|
||||
"type": {
|
||||
"type": "String"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"user/PtlLogin/ReqLogin": {
|
||||
"type": "Interface",
|
||||
"extends": [
|
||||
{
|
||||
"id": 0,
|
||||
"type": {
|
||||
"type": "Reference",
|
||||
"target": "base/BaseRequest"
|
||||
}
|
||||
}
|
||||
],
|
||||
"properties": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "username",
|
||||
"type": {
|
||||
"type": "String"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 1,
|
||||
"name": "password",
|
||||
"type": {
|
||||
"type": "String"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"user/PtlLogin/ResLogin": {
|
||||
"type": "Interface",
|
||||
"extends": [
|
||||
{
|
||||
"id": 0,
|
||||
"type": {
|
||||
"type": "Reference",
|
||||
"target": "base/BaseResponse"
|
||||
}
|
||||
}
|
||||
],
|
||||
"properties": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "__ssoToken",
|
||||
"type": {
|
||||
"type": "String"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"user/PtlLogout/ReqLogout": {
|
||||
"type": "Interface",
|
||||
"extends": [
|
||||
{
|
||||
"id": 0,
|
||||
"type": {
|
||||
"type": "Reference",
|
||||
"target": "base/BaseRequest"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"user/PtlLogout/ResLogout": {
|
||||
"type": "Interface",
|
||||
"extends": [
|
||||
{
|
||||
"id": 0,
|
||||
"type": {
|
||||
"type": "Reference",
|
||||
"target": "base/BaseResponse"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
};
|
@@ -0,0 +1,14 @@
|
||||
import { BaseConf, BaseRequest, BaseResponse } from '../base';
|
||||
|
||||
export interface ReqLogin extends BaseRequest {
|
||||
username: string,
|
||||
password: string
|
||||
}
|
||||
|
||||
export interface ResLogin extends BaseResponse {
|
||||
__ssoToken: string;
|
||||
}
|
||||
|
||||
export const conf: BaseConf = {
|
||||
|
||||
};
|
@@ -0,0 +1,13 @@
|
||||
import { BaseRequest, BaseResponse, BaseConf } from '../base'
|
||||
|
||||
export interface ReqLogout extends BaseRequest {
|
||||
|
||||
}
|
||||
|
||||
export interface ResLogout extends BaseResponse {
|
||||
|
||||
}
|
||||
|
||||
export const conf: BaseConf = {
|
||||
|
||||
};
|
Reference in New Issue
Block a user