From a73c6d7ae5ce1e7216bfb0967eede9f571b6ecea Mon Sep 17 00:00:00 2001 From: Carey Gumaer Date: Mon, 5 Feb 2024 16:25:07 -0500 Subject: [PATCH 1/5] add the redirectUrl argument and use it in loginUrl --- index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index e0ac58b..d8335b7 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,7 @@ * @function initLoginButton * @param {String} containerId The id property of the container element to place the login button / status in * @param {String} baseUrl The base URL of the MIT Open instance + * @param {String} redirectUrl The URL to redirect to after login is complete * @param {String} buttonText The text to show on the Login button * @param {String} buttonClass The CSS class(es) to assign to the button * @param {String} loggedInTextClass The CSS class(es) to assign to the logged-in status text @@ -11,6 +12,7 @@ export function initLoginButton( containerId, baseUrl, + redirectUrl, buttonText = "Login", buttonClass = "", loggedInTextClass = "", @@ -18,7 +20,7 @@ export function initLoginButton( const container = document.getElementById(containerId) const parsedBaseUrl = new URL(baseUrl) const currentUserUrl = `${parsedBaseUrl.origin}/api/v0/users/me/?format=json` - const loginUrl = `${parsedBaseUrl.origin}/login/ol-oidc/` + const loginUrl = `${parsedBaseUrl.origin}/login/ol-oidc/?redirect_url=${encodeURIComponent(redirectUrl)}` fetch(currentUserUrl, { method: "GET", credentials: "include", From 00d7538a6b2c39cce1a2167bc03dabd27fcee654 Mon Sep 17 00:00:00 2001 From: Carey Gumaer Date: Mon, 5 Feb 2024 16:41:32 -0500 Subject: [PATCH 2/5] set a default on the redirectUrl agument, and if it is not provided don't append the query string param --- index.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index d8335b7..d2368a3 100644 --- a/index.js +++ b/index.js @@ -12,7 +12,7 @@ export function initLoginButton( containerId, baseUrl, - redirectUrl, + redirectUrl = "", buttonText = "Login", buttonClass = "", loggedInTextClass = "", @@ -20,7 +20,9 @@ export function initLoginButton( const container = document.getElementById(containerId) const parsedBaseUrl = new URL(baseUrl) const currentUserUrl = `${parsedBaseUrl.origin}/api/v0/users/me/?format=json` - const loginUrl = `${parsedBaseUrl.origin}/login/ol-oidc/?redirect_url=${encodeURIComponent(redirectUrl)}` + const redirectUrlParam = + redirectUrl !== "" ? `?redirect_url=${redirectUrl}` : "" + const loginUrl = `${parsedBaseUrl.origin}/login/ol-oidc/${redirectUrlParam}` fetch(currentUserUrl, { method: "GET", credentials: "include", From 677bf66c9648e2fe903e1b90fbeb3dcccd142aa0 Mon Sep 17 00:00:00 2001 From: Carey Gumaer Date: Tue, 6 Feb 2024 11:35:40 -0500 Subject: [PATCH 3/5] URL encode redirect_url --- index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index d2368a3..7a374ac 100644 --- a/index.js +++ b/index.js @@ -22,7 +22,9 @@ export function initLoginButton( const currentUserUrl = `${parsedBaseUrl.origin}/api/v0/users/me/?format=json` const redirectUrlParam = redirectUrl !== "" ? `?redirect_url=${redirectUrl}` : "" - const loginUrl = `${parsedBaseUrl.origin}/login/ol-oidc/${redirectUrlParam}` + const loginUrl = `${parsedBaseUrl.origin}/login/ol-oidc/${encodeURIComponent( + redirectUrlParam, + )}` fetch(currentUserUrl, { method: "GET", credentials: "include", From 5768ae344ca70202621310d155f56cd7237ecc3c Mon Sep 17 00:00:00 2001 From: Carey Gumaer Date: Thu, 8 Feb 2024 14:18:12 -0500 Subject: [PATCH 4/5] only urlencode the param itself --- index.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 7a374ac..4c6a0d6 100644 --- a/index.js +++ b/index.js @@ -21,10 +21,8 @@ export function initLoginButton( const parsedBaseUrl = new URL(baseUrl) const currentUserUrl = `${parsedBaseUrl.origin}/api/v0/users/me/?format=json` const redirectUrlParam = - redirectUrl !== "" ? `?redirect_url=${redirectUrl}` : "" - const loginUrl = `${parsedBaseUrl.origin}/login/ol-oidc/${encodeURIComponent( - redirectUrlParam, - )}` + redirectUrl !== "" ? `?redirect_url=${encodeURIComponent(redirectUrl)}` : "" + const loginUrl = `${parsedBaseUrl.origin}/login/ol-oidc/${redirectUrlParam}` fetch(currentUserUrl, { method: "GET", credentials: "include", From 839af6d57b4eeb7e658260bf6d8e0c3f0323eb7b Mon Sep 17 00:00:00 2001 From: Carey Gumaer Date: Thu, 8 Feb 2024 14:43:27 -0500 Subject: [PATCH 5/5] param needs to be next, not redirect_url --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 4c6a0d6..51fe859 100644 --- a/index.js +++ b/index.js @@ -21,7 +21,7 @@ export function initLoginButton( const parsedBaseUrl = new URL(baseUrl) const currentUserUrl = `${parsedBaseUrl.origin}/api/v0/users/me/?format=json` const redirectUrlParam = - redirectUrl !== "" ? `?redirect_url=${encodeURIComponent(redirectUrl)}` : "" + redirectUrl !== "" ? `?next=${encodeURIComponent(redirectUrl)}` : "" const loginUrl = `${parsedBaseUrl.origin}/login/ol-oidc/${redirectUrlParam}` fetch(currentUserUrl, { method: "GET",