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

Enable passing scopes to provider #66

Merged
merged 7 commits into from
Mar 10, 2021
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
5 changes: 5 additions & 0 deletions src/GoTrueApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,17 +184,22 @@ export default class GoTrueApi {
* Generates the relevant login URL for a third-party provider.
* @param provider One of the providers supported by GoTrue.
* @param redirectTo A URL or mobile address to send the user to after they are confirmed.
* @param scopes A space-separated list of scopes granted to the OAuth application.
*/
getUrlForProvider(
provider: Provider,
options: {
redirectTo?: string
scopes?: string
}
) {
let urlParams: string[] = [`provider=${provider}`]
if (options?.redirectTo) {
urlParams.push(`redirect_to=${options.redirectTo}`)
}
if (options?.scopes) {
urlParams.push(`scopes=${options.scopes}`)
}
return `${this.url}/authorize?${urlParams.join('&')}`
}

Expand Down
11 changes: 10 additions & 1 deletion src/GoTrueClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,13 @@ export default class GoTrueClient {
* @param password The user's password.
* @param provider One of the providers supported by GoTrue.
* @param redirectTo A URL or mobile address to send the user to after they are confirmed.
* @param scopes A space-separated list of scopes granted to the OAuth application.
*/
async signIn(
{ email, password, provider }: UserCredentials,
options: {
redirectTo?: string
scopes?: string
} = {}
): Promise<{
session: Session | null
Expand All @@ -174,6 +176,7 @@ export default class GoTrueClient {
if (provider) {
return this._handleProviderSignIn(provider, {
redirectTo: options.redirectTo,
scopes: options.scopes,
})
}
throw new Error(`You must provide either an email or a third-party provider.`)
Expand Down Expand Up @@ -258,6 +261,7 @@ export default class GoTrueClient {
const error_description = getParameterByName('error_description')
if (error_description) throw new Error(error_description)

const provider_token = getParameterByName('provider_token')
const access_token = getParameterByName('access_token')
if (!access_token) throw new Error('No access_token detected.')
const expires_in = getParameterByName('expires_in')
Expand All @@ -271,6 +275,7 @@ export default class GoTrueClient {
if (error) throw error

const session: Session = {
provider_token,
access_token,
expires_in: parseInt(expires_in),
refresh_token,
Expand Down Expand Up @@ -361,9 +366,13 @@ export default class GoTrueClient {
provider: Provider,
options: {
redirectTo?: string
scopes?: string
} = {}
) {
const url: string = this.api.getUrlForProvider(provider, { redirectTo: options.redirectTo })
const url: string = this.api.getUrlForProvider(provider, {
redirectTo: options.redirectTo,
scopes: options.scopes,
})

try {
// try to open on the browser
Expand Down
1 change: 1 addition & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export type Provider = 'azure' | 'bitbucket' | 'facebook' | 'github' | 'gitlab'
export type AuthChangeEvent = 'SIGNED_IN' | 'SIGNED_OUT' | 'USER_UPDATED' | 'PASSWORD_RECOVERY'

export interface Session {
provider_token?: string | null
access_token: string
expires_in: number
refresh_token: string
Expand Down
8 changes: 8 additions & 0 deletions test/__snapshots__/provider.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@ exports[`signIn() with Provider 2`] = `"google"`;
exports[`signIn() with Provider can append a redirectUrl 1`] = `"http://localhost:9999/authorize?provider=google&redirect_to=https://localhost:9000/welcome"`;

exports[`signIn() with Provider can append a redirectUrl 2`] = `"google"`;

exports[`signIn() with Provider can append multiple options 1`] = `"http://localhost:9999/authorize?provider=github&redirect_to=https://localhost:9000/welcome&scopes=repo"`;

exports[`signIn() with Provider can append multiple options 2`] = `"github"`;

exports[`signIn() with Provider can append scopes 1`] = `"http://localhost:9999/authorize?provider=github&scopes=repo"`;

exports[`signIn() with Provider can append scopes 2`] = `"github"`;
29 changes: 29 additions & 0 deletions test/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,32 @@ test('signIn() with Provider can append a redirectUrl ', async () => {
expect(url).toMatchSnapshot()
expect(provider).toMatchSnapshot()
})

test('signIn() with Provider can append scopes', async () => {
let { error, url, provider } = await auth.signIn(
{
provider: 'github',
},
{
scopes: 'repo',
}
)
expect(error).toBeNull()
expect(url).toMatchSnapshot()
expect(provider).toMatchSnapshot()
})

test('signIn() with Provider can append multiple options', async () => {
let { error, url, provider } = await auth.signIn(
{
provider: 'github',
},
{
redirectTo: 'https://localhost:9000/welcome',
scopes: 'repo',
}
)
expect(error).toBeNull()
expect(url).toMatchSnapshot()
expect(provider).toMatchSnapshot()
})