Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(auth:cookie): fix cookie expires #1636

Merged
merged 2 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 31 additions & 17 deletions packages/auth/src/store/cookie-storage.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { CookieOptions, CookieService } from '@delon/util/browser';

import { CookieStorageStore } from './cookie-storage.service';
import { ITokenModel } from '../token/interface';

describe('auth: cookie-storage', () => {
let data: { [key: string]: any } = {};
const store = new CookieStorageStore({
put: jasmine.createSpy('put').and.callFake((key: string, value: string) => (data[key] = value)),
get: jasmine.createSpy('get').and.callFake((key: string) => data[key]),
remove: jasmine.createSpy('remove').and.callFake((key: string) => {
delete data[key];
})
} as any);
let putSpy: jasmine.Spy;
let store: CookieStorageStore;
const KEY = 'token';
const VALUE: ITokenModel = {
token: 'token data'
} as ITokenModel;

beforeEach(() => {
data = {};
putSpy = jasmine.createSpy('put').and.callFake((key: string, value: string) => (data[key] = value));
store = new CookieStorageStore({
put: putSpy,
get: jasmine.createSpy('get').and.callFake((key: string) => data[key]),
remove: jasmine.createSpy('remove').and.callFake((key: string) => {
delete data[key];
})
} as unknown as CookieService);
});

it('should be never return null', () => {
Expand All @@ -43,17 +48,26 @@ describe('auth: cookie-storage', () => {
expect(invalidRet.token).toBeUndefined();
});

it('#set', () => {
store.set(KEY, VALUE);
let ret = store.get(KEY);
expect(ret).not.toBeNull();
expect(ret.token).toBe(VALUE.token);
describe('#set', () => {
it('should be working', () => {
store.set(KEY, VALUE);
let ret = store.get(KEY);
expect(ret).not.toBeNull();
expect(ret.token).toBe(VALUE.token);

// when is null or undefined
store.set(KEY, undefined);
ret = store.get(KEY);
expect(ret).not.toBeNull();
expect(Object.keys(ret).length).toBe(0);
// when is null or undefined
store.set(KEY, undefined);
ret = store.get(KEY);
expect(ret).not.toBeNull();
expect(Object.keys(ret).length).toBe(0);
});
it('should be set expired', () => {
store.set(KEY, { ...VALUE, expired: 1000 * 3 });
const args = putSpy.calls.first().args;
expect(args.length).toBe(3);
const options = args[2] as CookieOptions;
expect(typeof options.expires === 'number' && options.expires > 0).toBe(true);
});
});

it('#remove', () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/auth/src/store/cookie-storage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export class CookieStorageStore implements IStore {
}

set(key: string, value: ITokenModel | null | undefined): boolean {
this.srv.put(key, value != null ? JSON.stringify(value) : '{}');
const expires = (value?.expired ?? 0) / 1e3;
this.srv.put(key, JSON.stringify(value ?? {}), { expires });
return true;
}

Expand Down