mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-29 18:16:17 +01:00 
			
		
		
		
	fix: sso redirect on /login & /api/login
This commit is contained in:
		| @@ -145,12 +145,18 @@ ajaxify = window.ajaxify || {}; | |||||||
| 				app.alertError('[[global:please_log_in]]'); | 				app.alertError('[[global:please_log_in]]'); | ||||||
| 				app.previousUrl = url; | 				app.previousUrl = url; | ||||||
| 				window.location.href = config.relative_path + '/login'; | 				window.location.href = config.relative_path + '/login'; | ||||||
| 			} else if ((status === 302 || status === 308) && typeof data.responseJSON === 'string') { | 			} else if (status === 302 || status === 308) { | ||||||
| 				ajaxifyTimer = undefined; | 				if (data.responseJSON && data.responseJSON.external) { | ||||||
| 				if (data.responseJSON.startsWith('http://') || data.responseJSON.startsWith('https://')) { | 					// this is used by sso plugins to redirect to the auth route | ||||||
| 					window.location.href = data.responseJSON; | 					// cant use ajaxify.go for /auth/sso routes | ||||||
| 				} else { | 					window.location.href = data.responseJSON.external; | ||||||
| 					ajaxify.go(data.responseJSON.slice(1), callback, quiet); | 				} else if (typeof data.responseJSON === 'string') { | ||||||
|  | 					ajaxifyTimer = undefined; | ||||||
|  | 					if (data.responseJSON.startsWith('http://') || data.responseJSON.startsWith('https://')) { | ||||||
|  | 						window.location.href = data.responseJSON; | ||||||
|  | 					} else { | ||||||
|  | 						ajaxify.go(data.responseJSON.slice(1), callback, quiet); | ||||||
|  | 					} | ||||||
| 				} | 				} | ||||||
| 			} | 			} | ||||||
| 		} else if (textStatus !== 'abort') { | 		} else if (textStatus !== 'abort') { | ||||||
|   | |||||||
| @@ -145,11 +145,20 @@ helpers.notAllowed = async function (req, res, error) { | |||||||
| }; | }; | ||||||
|  |  | ||||||
| helpers.redirect = function (res, url, permanent) { | helpers.redirect = function (res, url, permanent) { | ||||||
| 	if (res.locals.isAPI) { | 	let redirectUrl; | ||||||
| 		res.set('X-Redirect', encodeURI(url)).status(200).json(encodeURI(url)); | 	// this is used by sso plugins to redirect to the auth route | ||||||
|  | 	if (url.hasOwnProperty('external')) { | ||||||
|  | 		url.external = encodeURI(url.external); | ||||||
|  | 		redirectUrl = url.external; | ||||||
| 	} else { | 	} else { | ||||||
| 		const redirectUrl = url.startsWith('http://') || url.startsWith('https://') ? | 		url = encodeURI(url); | ||||||
| 			url : relative_path + url; | 		redirectUrl = url; | ||||||
|  | 	} | ||||||
|  | 	if (res.locals.isAPI) { | ||||||
|  | 		res.set('X-Redirect', redirectUrl).status(200).json(url); | ||||||
|  | 	} else { | ||||||
|  | 		redirectUrl = redirectUrl.startsWith('http://') || redirectUrl.startsWith('https://') ? | ||||||
|  | 			redirectUrl : relative_path + redirectUrl; | ||||||
| 		res.redirect(permanent ? 308 : 307, encodeURI(redirectUrl)); | 		res.redirect(permanent ? 308 : 307, encodeURI(redirectUrl)); | ||||||
| 	} | 	} | ||||||
| }; | }; | ||||||
|   | |||||||
| @@ -125,7 +125,7 @@ Controllers.login = async function (req, res) { | |||||||
| 	data.allowLocalLogin = hasLoginPrivilege || parseInt(req.query.local, 10) === 1; | 	data.allowLocalLogin = hasLoginPrivilege || parseInt(req.query.local, 10) === 1; | ||||||
|  |  | ||||||
| 	if (!data.allowLocalLogin && !data.allowRegistration && data.alternate_logins && data.authentication.length === 1) { | 	if (!data.allowLocalLogin && !data.allowRegistration && data.alternate_logins && data.authentication.length === 1) { | ||||||
| 		return helpers.redirect(res, data.authentication[0].url); | 		return helpers.redirect(res, { external: data.authentication[0].url }); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if (req.loggedIn) { | 	if (req.loggedIn) { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user