Browse Source

authentication flow updated

feature/ssr-migration-schematic
erdemcaygor 1 year ago
parent
commit
53774afdb6
  1. 2
      npm/ng-packs/apps/dev-app/src/app/app.config.server.ts
  2. 6
      npm/ng-packs/apps/dev-app/src/server.ts
  3. 37
      npm/ng-packs/packages/oauth/src/lib/services/browser-token-storage.service.ts
  4. 29
      npm/ng-packs/packages/oauth/src/lib/services/server-token-storage.service.ts

2
npm/ng-packs/apps/dev-app/src/app/app.config.server.ts

@ -26,7 +26,7 @@ const serverConfig: ApplicationConfig = {
}),
),
APP_ROUTE_PROVIDER,
provideAbpOAuth(),
provideAbpOAuth({ ssr: true }),
provideAbpThemeShared(),
provideSettingManagementConfig(),
provideAccountConfig(),

6
npm/ng-packs/apps/dev-app/src/server.ts

@ -36,14 +36,16 @@ export function app(): express.Express {
// All regular routes use the Angular engine
server.get('*', (req, res, next) => {
const { protocol, originalUrl, baseUrl, headers } = req;
commonEngine
.render({
bootstrap,
documentFilePath: indexHtml,
url: `${protocol}://${headers.host}${originalUrl}`,
publicPath: distFolder,
providers: [{ provide: APP_BASE_HREF, useValue: baseUrl }],
providers: [
{ provide: APP_BASE_HREF, useValue: baseUrl },
{ provide: 'cookies', useValue: JSON.stringify(req.headers.cookie) },
],
})
.then(html => res.send(html))
.catch(err => next(err));

37
npm/ng-packs/packages/oauth/src/lib/services/browser-token-storage.service.ts

@ -0,0 +1,37 @@
import { Injectable } from '@angular/core';
import { OAuthStorage } from 'angular-oauth2-oidc';
@Injectable({
providedIn: 'root',
})
export class BrowserTokenStorageService implements OAuthStorage {
getItem(key: string): string {
return this.readCookie(key);
}
removeItem(key: string): void {
this.removeCookie(key);
}
setItem(key: string, data: string): void {
console.log(key);
this.writeCookie(key, data);
}
readCookie(name: string): string | null {
if (typeof document === 'undefined') return null; // SSR'de yok
const match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
return match ? decodeURIComponent(match[2]) : null;
}
writeCookie(name: string, value: string, days = 7): void {
if (typeof document === 'undefined') return; // SSR'de yok
const expires = new Date(Date.now() + days * 86400000).toUTCString();
document.cookie = `${name}=${encodeURIComponent(value)}; expires=${expires}; path=/; Secure; SameSite=Lax`;
}
removeCookie(name: string): void {
if (typeof document === 'undefined') return; // SSR'de yok
document.cookie = `${name}=; Max-Age=0; path=/;`;
}
}

29
npm/ng-packs/packages/oauth/src/lib/services/server-token-storage.service.ts

@ -0,0 +1,29 @@
import { Inject, Injectable } from '@angular/core';
import { OAuthStorage } from 'angular-oauth2-oidc';
@Injectable({
providedIn: 'root',
})
export class ServerTokenStorageService implements OAuthStorage {
private cookies: Map<string, string>;
constructor(@Inject('cookies') c: any) {
console.log('cookies ---->>>>>>>', c);
const cookies = JSON.parse(c);
this.cookies = new Map<string, string>();
for (const cookie of cookies) {
this.cookies.set(cookie.key, cookie.value);
}
}
getItem(key: string): string {
if (this.cookies) {
return this.cookies.get(key);
}
return '';
}
removeItem(key: string): void {}
setItem(key: string, data: string): void {}
}
Loading…
Cancel
Save