-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
da66a29
commit 3c33fea
Showing
6 changed files
with
69 additions
and
88 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { HttpContext } from "@adonisjs/core/http"; | ||
|
||
import AuthSocialService from "#services/auth_social_service"; | ||
|
||
export default class SocialController { | ||
async redirect({ ally, params }: HttpContext) { | ||
await ally.use(params["provider"]).redirect(); | ||
} | ||
|
||
async callback(ctx: HttpContext) { | ||
await AuthSocialService.handleCallback(ctx); | ||
} | ||
} |
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,35 @@ | ||
import { SocialProviders } from "@adonisjs/ally/types"; | ||
import { HttpContext } from "@adonisjs/core/http"; | ||
|
||
import UserProvider from "#services/auth/user/user-provider"; | ||
import { retrieveRefererPath } from "#services/config/api-path"; | ||
import { BlEnv } from "#services/config/env"; | ||
|
||
async function handleCallback(ctx: HttpContext) { | ||
const provider: keyof SocialProviders = ctx.params["provider"]; | ||
const social = ctx.ally.use(provider); | ||
|
||
if (social.accessDenied() || social.stateMisMatch() || social.hasError()) { | ||
return ctx.response.redirect(`${BlEnv.CLIENT_URI}auth/social/failure`); | ||
} | ||
|
||
const user = await social.user(); | ||
|
||
const { accessToken, refreshToken } = await UserProvider.loginOrCreate( | ||
user.email, | ||
provider, | ||
user.id, | ||
); | ||
|
||
return ctx.response.redirect( | ||
`${ | ||
retrieveRefererPath(ctx.request.headers()) ?? BlEnv.CLIENT_URI | ||
}auth/token?access_token=${accessToken}&refresh_token=${refreshToken}`, | ||
); | ||
} | ||
|
||
const AuthSocialService = { | ||
handleCallback, | ||
}; | ||
|
||
export default AuthSocialService; |
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 |
---|---|---|
|
@@ -79,7 +79,7 @@ test.group("UserProvider", (group) => { | |
|
||
return expect( | ||
UserProvider.loginOrCreate("[email protected]", "local", "abcdefg"), | ||
).to.eventually.be.eql({ user: user, tokens: tokens }); | ||
).to.eventually.be.eql(tokens); | ||
}); | ||
|
||
test("loginOrCreate() - should resolve with a user object and tokens", async () => { | ||
|
@@ -93,6 +93,6 @@ test.group("UserProvider", (group) => { | |
|
||
return expect( | ||
UserProvider.loginOrCreate("[email protected]", "local", "abcdefg"), | ||
).to.eventually.be.eql({ user: user, tokens: tokens }); | ||
).to.eventually.be.eql(tokens); | ||
}); | ||
}); |
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