Skip to content

Commit

Permalink
feat: 添加新的认证模式
Browse files Browse the repository at this point in the history
  • Loading branch information
devcui committed Sep 29, 2024
1 parent 6f9beff commit d7db0d5
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/auth/src/auth.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { YunzaiAuthConfig, YunzaiConfigService } from '@yelon/util/config';

export const AUTH_DEFAULT_CONFIG: YunzaiAuthConfig = {
auto: true,
store_key: `_yz_token`,
token_invalid_redirect: true,
token_exp_offset: 10,
Expand Down
21 changes: 21 additions & 0 deletions packages/bis/src/startup.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
useLocalStorageCurrent,
useLocalStorageDefaultRoute,
useLocalStorageHeader,
useLocalStorageNeedAuth,
useLocalStorageProjectInfo,
useLocalStorageTenant,
useLocalStorageUser,
Expand All @@ -39,6 +40,7 @@ export function provideYunzaiStartup(): Provider[] {
}
];
}

@Injectable()
export class YunzaiStartupService {
private readonly config = mergeBisConfig(inject(YunzaiConfigService));
Expand All @@ -52,6 +54,12 @@ export class YunzaiStartupService {
private readonly win = inject(WINDOW);
private readonly configService = inject(YunzaiConfigService);

casLogin(): Observable<void> {
const [setNeedAuth] = useLocalStorageNeedAuth();
setNeedAuth(true);
return this.load();
}

load(): Observable<void> {
let defaultLang: string = this.settingService.layout.lang || this.i18n.defaultLang;
const [setTenant] = useLocalStorageTenant();
Expand All @@ -60,6 +68,18 @@ export class YunzaiStartupService {
const [setProject] = useLocalStorageProjectInfo();
const [setDefaultRoute] = useLocalStorageDefaultRoute();
const [setCurrent] = useLocalStorageCurrent();
const [setNeedAuth, getNeedAuth] = useLocalStorageNeedAuth();

// 如果不需要认证 且 跳过自动认证, 加载静态国际化文件结束流程
if (!getNeedAuth() && !this.configService.get('auth')?.auto) {
return this.i18n.loadLocaleData(defaultLang).pipe(
map((langData: NzSafeAny) => {
this.i18n.use(defaultLang, langData);
return void 0;
})
);
}

return this.token().pipe(
mergeMap((token: ITokenModel) => {
this.configService.set('auth', {
Expand Down Expand Up @@ -136,6 +156,7 @@ export class YunzaiStartupService {
setDefaultRoute('/displayIndex');
}
}
setNeedAuth(false);
return of(void 0);
})
);
Expand Down
4 changes: 4 additions & 0 deletions packages/theme/src/services/i18n/yunzai-i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ export class YunzaiHttpI18NService extends YunzaiI18nBaseService implements OnDe
}
}

loadLocaleData(lang: string): Observable<NzSafeAny> {
return this.http.get(`./assets/tmp/i18n/${lang}.json`);
}

use(lang: string, data: Record<string, unknown>): void {
if (this._currentLang === lang) return;
this._data = this.flatData(data, []);
Expand Down
4 changes: 4 additions & 0 deletions packages/util/config/auth/auth.type.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export interface YunzaiAuthConfig {
/**
* 是否自动完成认证流程
*/
auto?: boolean;
/**
* 存储KEY值,默认:`_token`
*/
Expand Down
7 changes: 7 additions & 0 deletions packages/util/mcache/localStorageUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const YZ_HEADER_KEY = '_yz_header';
export const YZ_HEADER_TYPE_KEY = '_yz_header_type';
export const YZ_DEFAULT_ROUTE_KEY = '_yz_default_route';
export const YZ_TENANT_KEY = '_yz_tenant';
export const YZ_NEED_AUTH_KEY = '_yz_need_auth';
// export const YZ_LANGS_KEY = '_yz_langs';
// export const YZ_LANG_KEY = '_yz_lang';

Expand Down Expand Up @@ -71,3 +72,9 @@ export function useLocalStorageTenant(): [returnSet<string>, returnGet<string>]
const getFn: returnGet<string> = () => get<string>(YZ_TENANT_KEY);
return [setFn, getFn];
}

export function useLocalStorageNeedAuth(): [returnSet<boolean>, returnGet<boolean>] {
const setFn: returnSet<boolean> = data => set(YZ_NEED_AUTH_KEY, data);
const getFn: returnGet<boolean> = () => get<boolean>(YZ_NEED_AUTH_KEY);
return [setFn, getFn];
}

0 comments on commit d7db0d5

Please sign in to comment.