Skip to content

Commit

Permalink
fix #268
Browse files Browse the repository at this point in the history
  • Loading branch information
Ansonhkg committed Nov 28, 2023
1 parent b5e7514 commit 1c4f6e8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
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

0 comments on commit 1c4f6e8

Please sign in to comment.