fix: 登录/注册支持 redirect 参数,未登录跳转自动携带来源页
- login.js/register.js 登录成功后优先跳转 redirect 参数指定页面 - 新增 common.RedirectToLogin 辅助函数,自动携带当前路径 - 替换所有 9 处硬编码 /auth/login 重定向
This commit is contained in:
@ -1,6 +1,9 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
"metazone.cc/metalab/internal/model"
|
"metazone.cc/metalab/internal/model"
|
||||||
@ -74,6 +77,16 @@ func BuildAdminPageData(c *gin.Context, extra gin.H) gin.H {
|
|||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RedirectToLogin 重定向到登录页,携带当前页面路径作为 redirect 参数
|
||||||
|
func RedirectToLogin(c *gin.Context) {
|
||||||
|
returnURL := url.QueryEscape(c.Request.URL.Path)
|
||||||
|
if c.Request.URL.RawQuery != "" {
|
||||||
|
returnURL = url.QueryEscape(c.Request.URL.Path + "?" + c.Request.URL.RawQuery)
|
||||||
|
}
|
||||||
|
c.Redirect(http.StatusFound, fmt.Sprintf("/auth/login?redirect=%s", returnURL))
|
||||||
|
c.Abort()
|
||||||
|
}
|
||||||
|
|
||||||
// injectSiteInfo 从 context 注入站点展示信息
|
// injectSiteInfo 从 context 注入站点展示信息
|
||||||
func injectSiteInfo(c *gin.Context, data gin.H) {
|
func injectSiteInfo(c *gin.Context, data gin.H) {
|
||||||
if framework, exists := c.Get("site_framework"); exists {
|
if framework, exists := c.Get("site_framework"); exists {
|
||||||
|
|||||||
@ -14,8 +14,7 @@ import (
|
|||||||
func (mc *MessageController) MessagesPage(c *gin.Context) {
|
func (mc *MessageController) MessagesPage(c *gin.Context) {
|
||||||
uidVal, exists := c.Get("uid")
|
uidVal, exists := c.Get("uid")
|
||||||
if !exists {
|
if !exists {
|
||||||
c.Redirect(http.StatusFound, "/auth/login")
|
common.RedirectToLogin(c)
|
||||||
c.Abort()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
uid := uidVal.(uint)
|
uid := uidVal.(uint)
|
||||||
|
|||||||
@ -58,8 +58,7 @@ func NewSettingsController(authService profileProvider, avatarService avatarProv
|
|||||||
func (sc *SettingsController) SettingsPage(c *gin.Context) {
|
func (sc *SettingsController) SettingsPage(c *gin.Context) {
|
||||||
uidVal, exists := c.Get("uid")
|
uidVal, exists := c.Get("uid")
|
||||||
if !exists {
|
if !exists {
|
||||||
c.Redirect(http.StatusFound, "/auth/login")
|
common.RedirectToLogin(c)
|
||||||
c.Abort()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
uid := uidVal.(uint)
|
uid := uidVal.(uint)
|
||||||
|
|||||||
@ -23,7 +23,7 @@ func NewSpaceController(spaceService spaceUseCase) *SpaceController {
|
|||||||
func (ctrl *SpaceController) MySpace(c *gin.Context) {
|
func (ctrl *SpaceController) MySpace(c *gin.Context) {
|
||||||
uid, _, ok := common.GetGinUser(c)
|
uid, _, ok := common.GetGinUser(c)
|
||||||
if !ok {
|
if !ok {
|
||||||
c.Redirect(http.StatusFound, "/auth/login")
|
common.RedirectToLogin(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.Redirect(http.StatusFound, "/space/"+strconv.FormatUint(uint64(uid), 10))
|
c.Redirect(http.StatusFound, "/space/"+strconv.FormatUint(uint64(uid), 10))
|
||||||
|
|||||||
@ -28,7 +28,7 @@ func NewStudioController(ps studioUseCase) *StudioController {
|
|||||||
func (ctrl *StudioController) OverviewPage(c *gin.Context) {
|
func (ctrl *StudioController) OverviewPage(c *gin.Context) {
|
||||||
uid, _, ok := common.GetGinUser(c)
|
uid, _, ok := common.GetGinUser(c)
|
||||||
if !ok {
|
if !ok {
|
||||||
c.Redirect(http.StatusFound, "/auth/login")
|
common.RedirectToLogin(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,7 +56,7 @@ func (ctrl *StudioController) OverviewPage(c *gin.Context) {
|
|||||||
func (ctrl *StudioController) PostsPage(c *gin.Context) {
|
func (ctrl *StudioController) PostsPage(c *gin.Context) {
|
||||||
uid, _, ok := common.GetGinUser(c)
|
uid, _, ok := common.GetGinUser(c)
|
||||||
if !ok {
|
if !ok {
|
||||||
c.Redirect(http.StatusFound, "/auth/login")
|
common.RedirectToLogin(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,7 +96,7 @@ func (ctrl *StudioController) PostsPage(c *gin.Context) {
|
|||||||
func (ctrl *StudioController) DraftsPage(c *gin.Context) {
|
func (ctrl *StudioController) DraftsPage(c *gin.Context) {
|
||||||
uid, _, ok := common.GetGinUser(c)
|
uid, _, ok := common.GetGinUser(c)
|
||||||
if !ok {
|
if !ok {
|
||||||
c.Redirect(http.StatusFound, "/auth/login")
|
common.RedirectToLogin(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,7 +131,7 @@ func (ctrl *StudioController) DraftsPage(c *gin.Context) {
|
|||||||
func (ctrl *StudioController) WritePage(c *gin.Context) {
|
func (ctrl *StudioController) WritePage(c *gin.Context) {
|
||||||
uid, _, ok := common.GetGinUser(c)
|
uid, _, ok := common.GetGinUser(c)
|
||||||
if !ok {
|
if !ok {
|
||||||
c.Redirect(http.StatusFound, "/auth/login")
|
common.RedirectToLogin(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -171,7 +171,7 @@ func (ctrl *StudioController) WritePage(c *gin.Context) {
|
|||||||
func (ctrl *StudioController) AnalyticsPage(c *gin.Context) {
|
func (ctrl *StudioController) AnalyticsPage(c *gin.Context) {
|
||||||
uid, _, ok := common.GetGinUser(c)
|
uid, _, ok := common.GetGinUser(c)
|
||||||
if !ok {
|
if !ok {
|
||||||
c.Redirect(http.StatusFound, "/auth/login")
|
common.RedirectToLogin(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -86,6 +86,5 @@ func blockAccess(c *gin.Context, path string) {
|
|||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.Redirect(http.StatusFound, "/auth/login")
|
common.RedirectToLogin(c)
|
||||||
c.Abort()
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,6 +15,13 @@
|
|||||||
|
|
||||||
if (!loginForm) return;
|
if (!loginForm) return;
|
||||||
|
|
||||||
|
// 读取 redirect 参数(登录成功后跳转目标)
|
||||||
|
function getRedirect() {
|
||||||
|
var m = window.location.search.match(/[?&]redirect=([^&]+)/);
|
||||||
|
if (m) return decodeURIComponent(m[1]);
|
||||||
|
return '/';
|
||||||
|
}
|
||||||
|
|
||||||
function checkFormValidity() {
|
function checkFormValidity() {
|
||||||
var email = emailInput.value.trim();
|
var email = emailInput.value.trim();
|
||||||
var password = passwordInput.value;
|
var password = passwordInput.value;
|
||||||
@ -81,7 +88,7 @@
|
|||||||
} else if (xhr.status === 200 && resp && resp.success) {
|
} else if (xhr.status === 200 && resp && resp.success) {
|
||||||
window.MetaLab.toast(toast, '登录成功', false);
|
window.MetaLab.toast(toast, '登录成功', false);
|
||||||
setTimeout(function () {
|
setTimeout(function () {
|
||||||
window.location.href = '/';
|
window.location.href = getRedirect();
|
||||||
}, 800);
|
}, 800);
|
||||||
} else {
|
} else {
|
||||||
loginInProgress = false;
|
loginInProgress = false;
|
||||||
@ -112,7 +119,7 @@
|
|||||||
if (xhr.status === 200 && resp && resp.success) {
|
if (xhr.status === 200 && resp && resp.success) {
|
||||||
window.MetaLab.toast(toast, resp.message || '账号已恢复,欢迎回来', false);
|
window.MetaLab.toast(toast, resp.message || '账号已恢复,欢迎回来', false);
|
||||||
setTimeout(function () {
|
setTimeout(function () {
|
||||||
window.location.href = '/';
|
window.location.href = getRedirect();
|
||||||
}, 800);
|
}, 800);
|
||||||
} else {
|
} else {
|
||||||
loginInProgress = false;
|
loginInProgress = false;
|
||||||
|
|||||||
@ -32,6 +32,13 @@
|
|||||||
// Guard: exit if not on auth page
|
// Guard: exit if not on auth page
|
||||||
if (!registerForm) return;
|
if (!registerForm) return;
|
||||||
|
|
||||||
|
// 读取 redirect 参数(注册成功后跳转目标)
|
||||||
|
function getRedirect() {
|
||||||
|
var m = window.location.search.match(/[?&]redirect=([^&]+)/);
|
||||||
|
if (m) return decodeURIComponent(m[1]);
|
||||||
|
return '/';
|
||||||
|
}
|
||||||
|
|
||||||
var countdown = 60;
|
var countdown = 60;
|
||||||
var timerInterval = null;
|
var timerInterval = null;
|
||||||
var reachedBottom = false;
|
var reachedBottom = false;
|
||||||
@ -287,7 +294,7 @@
|
|||||||
// Token 已通过 HttpOnly Cookie 自动设置,JS 无需处理
|
// Token 已通过 HttpOnly Cookie 自动设置,JS 无需处理
|
||||||
// 注册即登录,跳转首页
|
// 注册即登录,跳转首页
|
||||||
setTimeout(function () {
|
setTimeout(function () {
|
||||||
window.location.href = '/';
|
window.location.href = getRedirect();
|
||||||
}, 1500);
|
}, 1500);
|
||||||
} else {
|
} else {
|
||||||
var msg = (resp && resp.message) ? resp.message : '注册失败,请稍后重试';
|
var msg = (resp && resp.message) ? resp.message : '注册失败,请稍后重试';
|
||||||
|
|||||||
Reference in New Issue
Block a user