-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add .auth/login/{provider}/callback rule
- Loading branch information
1 parent
87299e1
commit 31b2ac8
Showing
2 changed files
with
71 additions
and
0 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,19 @@ | ||
{ | ||
"bindings": [ | ||
{ | ||
"authLevel": "function", | ||
"type": "httpTrigger", | ||
"direction": "in", | ||
"name": "req", | ||
"methods": [ | ||
"get" | ||
], | ||
"route": ".auth/login/{provider}/callback" | ||
}, | ||
{ | ||
"type": "http", | ||
"direction": "out", | ||
"name": "res" | ||
} | ||
] | ||
} |
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,52 @@ | ||
const { response } = require("../utils"); | ||
const { default: fetch } = require("node-fetch"); | ||
|
||
module.exports = async function (context, req) { | ||
let { state, code } = req.query; | ||
|
||
state = decodeURIComponent(state); | ||
|
||
const client_id = process.env.GITHUB_CLIENT_ID; | ||
const client_secret = process.env.GITHUB_CLIENT_SECRET; | ||
const oauthUri = `https://github.com/login/oauth/access_token?client_id=${client_id}&client_secret=${client_secret}&code=${code}&state=${state}`; | ||
const githubOauthResponse = await fetch(oauthUri, { | ||
method: "POST", | ||
headers: { | ||
accept: "application/json", | ||
}, | ||
}); | ||
|
||
const token = await githubOauthResponse.json(); | ||
|
||
const location = "http://localhost:4242/.auth/login/done"; | ||
context.res = response({ | ||
context, | ||
status: 302, | ||
cookies: [ | ||
{ | ||
name: "Nonce", | ||
value: "deleted", | ||
path: "/", | ||
expires: new Date('Thu, 01 Jan 1970 00:00:00 GMT'), | ||
}, | ||
{ | ||
name: "RedirectCount", | ||
value: "deleted", | ||
path: "/", | ||
expires: new Date('Thu, 01 Jan 1970 00:00:00 GMT'), | ||
}, | ||
{ | ||
name: "AppServiceAuthSession", | ||
value: "UUUUU", | ||
path: "/", | ||
secure: true, | ||
HttpOnly: true, | ||
SameSite: "None", | ||
}, | ||
], | ||
headers: { | ||
location, | ||
token | ||
}, | ||
}); | ||
}; |