-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(browser): sign-in session storage
- Loading branch information
Showing
5 changed files
with
125 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { NormalizeKeyPaths } from '@silverhand/essentials'; | ||
import get from 'lodash.get'; | ||
|
||
const logtoClientErrorCodes = Object.freeze({ | ||
sign_in_session: { | ||
not_found: 'Sign-in session not found.', | ||
}, | ||
}); | ||
|
||
export type LogtoClientErrorCode = NormalizeKeyPaths<typeof logtoClientErrorCodes>; | ||
|
||
const getMessageByErrorCode = (errorCode: LogtoClientErrorCode): string => { | ||
// TODO: linear issue LOG-1419 | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
const message = get(logtoClientErrorCodes, errorCode); | ||
|
||
if (typeof message === 'string') { | ||
return message; | ||
} | ||
|
||
return errorCode; | ||
}; | ||
|
||
export class LogtoClientError extends Error { | ||
code: LogtoClientErrorCode; | ||
data: unknown; | ||
|
||
constructor(code: LogtoClientErrorCode, data?: unknown) { | ||
super(getMessageByErrorCode(code)); | ||
this.code = code; | ||
this.data = data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,67 @@ | ||
import LogtoClient from './index'; | ||
import { assert } from 'superstruct'; | ||
|
||
import LogtoClient, { | ||
getLogtoKey, | ||
LogtoSignInSessionItem, | ||
LogtoSignInSessionItemSchema, | ||
} from './index'; | ||
|
||
const clientId = 'client_id_value'; | ||
const endpoint = 'https://logto.dev'; | ||
const authorizationEndpoint = `${endpoint}/oidc/auth`; | ||
const mockedCodeVerifier = 'code_verifier_value'; | ||
const mockedState = 'state_value'; | ||
const mockedSignInUri = `${authorizationEndpoint}?foo=bar`; | ||
const redirectUri = 'http://localhost:3000/callback'; | ||
const requester = jest.fn(); | ||
const signInUri = `${authorizationEndpoint}?foo=bar`; | ||
|
||
jest.mock('@logto/js', () => { | ||
return { | ||
...jest.requireActual('@logto/js'), | ||
fetchOidcConfig: async () => ({ authorizationEndpoint }), | ||
generateSignInUri: () => signInUri, | ||
}; | ||
}); | ||
|
||
jest.mock('@logto/js', () => ({ | ||
...jest.requireActual('@logto/js'), | ||
fetchOidcConfig: async () => ({ authorizationEndpoint }), | ||
generateCodeChallenge: jest.fn().mockResolvedValue('code_challenge_value'), | ||
generateCodeVerifier: jest.fn(() => mockedCodeVerifier), | ||
generateSignInUri: jest.fn(() => mockedSignInUri), | ||
generateState: jest.fn(() => mockedState), | ||
})); | ||
|
||
describe('LogtoClient', () => { | ||
test('constructor', () => { | ||
expect(() => new LogtoClient({ endpoint, clientId, requester })).not.toThrow(); | ||
}); | ||
|
||
describe('signIn', () => { | ||
test('sessionStorage should contain correct properties', async () => { | ||
const logtoClient = new LogtoClient({ endpoint, clientId, requester }); | ||
const originalRedirectUri = 'http://localhost:3000/callback'; | ||
await logtoClient.signIn(originalRedirectUri); | ||
|
||
const jsonItem = sessionStorage.getItem(getLogtoKey(clientId)); | ||
expect(jsonItem).not.toBeNull(); | ||
|
||
if (!jsonItem) { | ||
return; | ||
} | ||
|
||
const parseJsonItem = (): unknown => JSON.parse(jsonItem); | ||
expect(parseJsonItem).not.toThrow(); | ||
|
||
const item: unknown = parseJsonItem(); | ||
expect(() => { | ||
assert(item, LogtoSignInSessionItemSchema); | ||
}).not.toThrow(); | ||
|
||
const logtoSignInSessionItem = item as LogtoSignInSessionItem; | ||
const { redirectUri, codeVerifier, state } = logtoSignInSessionItem; | ||
|
||
expect(redirectUri).toEqual(originalRedirectUri); | ||
expect(codeVerifier).toEqual(mockedCodeVerifier); | ||
expect(state).toEqual(mockedState); | ||
}); | ||
|
||
test('window.location should be correct signInUri', async () => { | ||
const logtoClient = new LogtoClient({ endpoint, clientId, requester }); | ||
await logtoClient.signIn(redirectUri); | ||
expect(window.location.toString()).toEqual(signInUri); | ||
expect(window.location.toString()).toEqual(mockedSignInUri); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.