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

Feature/lit 1985 request for spa friendly oauth redirection mechanism in lit #269

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
4 changes: 3 additions & 1 deletion packages/lit-auth-client/src/lib/providers/BaseProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ export abstract class BaseProvider {
*
* @template T - Type representing the specific options for the authenticate method
* @param {T} [options] - Optional parameters that vary based on the provider
* @param {(currentUrl: string, redirectUri: string) => boolean} [urlCheckCallback] - Optional callback to handle authentication data or errors
* @returns {Promise<AuthMethod>} - Auth method object that contains authentication data
*/
abstract authenticate<T extends AuthenticateOptions>(
options?: T
options?: T,
urlCheckCallback?: (currentUrl: string, redirectUri: string) => boolean
): Promise<AuthMethod>;

/**
Expand Down
27 changes: 21 additions & 6 deletions packages/lit-auth-client/src/lib/providers/GoogleProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
AuthMethod,
AuthenticateOptions,
BaseProviderOptions,
OAuthProviderOptions,
} from '@lit-protocol/types';
Expand Down Expand Up @@ -28,23 +29,37 @@ export default class GoogleProvider extends BaseProvider {
/**
* Redirect user to the Lit's Google login page
*
* @param {Function} [callback] - Optional callback to handle login URL
* @returns {Promise<void>} - Redirects user to Lit login page
*/
public async signIn(): Promise<void> {
public async signIn(callback?: (url: string) => void): Promise<void> {
// Get login url
const loginUrl = await prepareLoginUrl('google', this.redirectUri);
// Redirect to login url
window.location.assign(loginUrl);

// If callback is provided, use it. Otherwise, redirect to login url
if (callback) {
callback(loginUrl);
} else {
window.location.assign(loginUrl);
}
}

/**
* Validate the URL parameters returned from Lit's login server and return the authentication data
*
* @returns {Promise<AuthMethod>} - Auth method object that contains OAuth token
*/
public async authenticate(): Promise<AuthMethod> {
// Check if current url matches redirect uri
if (!window.location.href.startsWith(this.redirectUri)) {
public async authenticate<T extends AuthenticateOptions>(
_?: T,
urlCheckCallback?: (currentUrl: string, redirectUri: string) => boolean
): Promise<AuthMethod> {

// Check if current url matches redirect uri using the callback if provided
const isUrlValid = urlCheckCallback
? urlCheckCallback(window.location.href, this.redirectUri)
: window.location.href.startsWith(this.redirectUri);

if (!isUrlValid) {
throw new Error(
`Current url "${window.location.href}" does not match provided redirect uri "${this.redirectUri}"`
);
Expand Down
Loading