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

Add support for a redirect URL. #15

Merged
merged 3 commits into from
Feb 2, 2024
Merged
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
19 changes: 14 additions & 5 deletions src/auth.toit
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,16 @@ class Auth:
--token_type=response["token_type"]
client_.set_session_ session

sign_in --provider/string --ui/Ui --open_browser/bool=true -> none:
/**
Signs in using an Oauth provider.

The user is redirected to the provider's authentication page and then back to the
a localhost URL so that the program can receive the access token. If a
$redirect_url is provided, the page redirects to that URL afterwards with the
same fragment that this program received. This can be used to provide nicer
success or error messages.
*/
sign_in --provider/string --ui/Ui --open_browser/bool=true --redirect_url/string?=null -> none:
network := net.open
try:
server_socket := network.tcp_listen 0
Expand Down Expand Up @@ -151,10 +160,9 @@ class Auth:
writer.write "You can close this window now."
session_latch.set true
else if request.path.starts_with "/auth":
// TODO(florian): extract error if there:
// ```
// http://localhost:41055/auth?error=server_error&error_description=Database+error+saving+new+user
// ```
redirect_code := ""
if redirect_url:
redirect_code = """window.location.href = "$redirect_url" + window.location.hash;"""
writer.write """
<html>
<body>
Expand All @@ -165,6 +173,7 @@ class Auth:
const req = new XMLHttpRequest();
req.addEventListener("load", function() {
document.getElementById("body").innerHTML = "You can close this window now.";
$redirect_code
});
req.open("GET", "http://localhost:$port/success?" + window.location.hash.substring(1));
req.send();
Expand Down
Loading