Skip to content

Commit 6d4f345

Browse files
committed
Use custom struct as localization key
1 parent 3abb766 commit 6d4f345

File tree

5 files changed

+224
-86
lines changed

5 files changed

+224
-86
lines changed

authboss.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,16 @@ func (a *Authboss) VerifyPassword(user AuthableUser, password string) error {
104104
// Localizef is a helper to translate a key using the translator
105105
// If the localizer is nil or returns an empty string,
106106
// then the original text will be returned using [fmt.Sprintf] to interpolate the args.
107-
func (a *Authboss) Localizef(ctx context.Context, text string, args ...any) string {
107+
func (a *Authboss) Localizef(ctx context.Context, key LocalizationKey, args ...any) string {
108108
if a.Config.Core.Localizer == nil {
109-
return fmt.Sprintf(text, args...)
109+
return fmt.Sprintf(key.Default, args...)
110110
}
111111

112-
if translated := a.Config.Core.Localizer.Localizef(ctx, text, args...); translated != "" {
112+
if translated := a.Config.Core.Localizer.Localizef(ctx, key, args...); translated != "" {
113113
return translated
114114
}
115115

116-
return fmt.Sprintf(text, args...)
116+
return fmt.Sprintf(key.Default, args...)
117117
}
118118

119119
// VerifyPassword uses authboss mechanisms to check that a password is correct.

localizer.go

+169-31
Original file line numberDiff line numberDiff line change
@@ -7,55 +7,193 @@ import (
77
type Localizer interface {
88
// Get the translation for the given text in the given context.
99
// If no translation is found, an empty string should be returned.
10-
Localizef(ctx context.Context, txt string, args ...any) string
10+
Localizef(ctx context.Context, key LocalizationKey, args ...any) string
1111
}
1212

13-
// Translation constants
14-
const (
15-
TxtSuccess = "success"
13+
type LocalizationKey struct {
14+
ID string
15+
Default string
16+
}
17+
18+
var (
19+
TxtSuccess = LocalizationKey{
20+
ID: "Success",
21+
Default: "success",
22+
}
1623

1724
// Used in the auth module
18-
TxtInvalidCredentials = "Invalid Credentials"
19-
TxtAuthFailed = "Please login"
25+
TxtInvalidCredentials = LocalizationKey{
26+
ID: "InvalidCredentials",
27+
Default: "Invalid Credentials",
28+
}
29+
TxtAuthFailed = LocalizationKey{
30+
ID: "AuthFailed",
31+
Default: "Please login",
32+
}
2033

2134
// Used in the register module
22-
TxtUserAlreadyExists = "User already exists"
23-
TxtRegisteredAndLoggedIn = "Account successfully created, you are now logged in"
35+
TxtUserAlreadyExists = LocalizationKey{
36+
ID: "UserAlreadyExists",
37+
Default: "User already exists",
38+
}
39+
TxtRegisteredAndLoggedIn = LocalizationKey{
40+
ID: "RegisteredAndLoggedIn",
41+
Default: "Account successfully created, you are now logged in",
42+
}
2443

2544
// Used in the confirm module
26-
TxtConfirmYourAccount = "Please verify your account, an e-mail has been sent to you."
27-
TxtAccountNotConfirmed = "Your account has not been confirmed, please check your e-mail."
28-
TxtInvalidConfirmToken = "Your confirmation token is invalid."
29-
TxtConfrimationSuccess = "You have successfully confirmed your account."
30-
TxtConfirmEmailSubject = "Confirm New Account"
45+
TxtConfirmYourAccount = LocalizationKey{
46+
ID: "ConfirmYourAccount",
47+
Default: "Please verify your account, an e-mail has been sent to you.",
48+
}
49+
TxtAccountNotConfirmed = LocalizationKey{
50+
ID: "AccountNotConfirmed",
51+
Default: "Your account has not been confirmed, please check your e-mail.",
52+
}
53+
TxtInvalidConfirmToken = LocalizationKey{
54+
ID: "InvalidConfirmToken",
55+
Default: "Your confirmation token is invalid.",
56+
}
57+
TxtConfrimationSuccess = LocalizationKey{
58+
ID: "ConfrimationSuccess",
59+
Default: "You have successfully confirmed your account.",
60+
}
61+
TxtConfirmEmailSubject = LocalizationKey{
62+
ID: "ConfirmEmailSubject",
63+
Default: "Confirm New Account",
64+
}
3165

3266
// Used in the lock module
33-
TxtLocked = "Your account has been locked, please contact the administrator."
67+
TxtLocked = LocalizationKey{
68+
ID: "Locked",
69+
Default: "Your account has been locked, please contact the administrator.",
70+
}
3471

3572
// Used in the logout module
36-
TxtLoggedOut = "You have been logged out"
73+
TxtLoggedOut = LocalizationKey{
74+
ID: "LoggedOut",
75+
Default: "You have been logged out",
76+
}
3777

3878
// Used in the oauth2 module
39-
TxtOAuth2LoginOK = "Logged in successfully with %s."
40-
TxtOAuth2LoginNotOK = "%s login cancelled or failed"
79+
TxtOAuth2LoginOK = LocalizationKey{
80+
ID: "OAuth2LoginOK",
81+
Default: "Logged in successfully with %s.",
82+
}
83+
TxtOAuth2LoginNotOK = LocalizationKey{
84+
ID: "OAuth2LoginNotOK",
85+
Default: "%s login cancelled or failed",
86+
}
4187

4288
// Used in the recover module
43-
TxtRecoverInitiateSuccessFlash = "An email has been sent to you with further instructions on how to reset your password."
44-
TxtPasswordResetEmailSubject = "Password Reset"
45-
TxtRecoverSuccessMsg = "Successfully updated password"
46-
TxtRecoverAndLoginSuccessMsg = "Successfully updated password and logged in"
89+
TxtRecoverInitiateSuccessFlash = LocalizationKey{
90+
ID: "RecoverInitiateSuccessFlash",
91+
Default: "An email has been sent to you with further instructions on how to reset your password.",
92+
}
93+
TxtPasswordResetEmailSubject = LocalizationKey{
94+
ID: "PasswordResetEmailSubject",
95+
Default: "Password Reset",
96+
}
97+
TxtRecoverSuccessMsg = LocalizationKey{
98+
ID: "RecoverSuccessMsg",
99+
Default: "Successfully updated password",
100+
}
101+
TxtRecoverAndLoginSuccessMsg = LocalizationKey{
102+
ID: "RecoverAndLoginSuccessMsg",
103+
Default: "Successfully updated password and logged in",
104+
}
47105

48106
// Used in the otp module
49-
TxtTooManyOTPs = "You cannot have more than %d one time passwords"
107+
TxtTooManyOTPs = LocalizationKey{
108+
ID: "TooManyOTPs",
109+
Default: "You cannot have more than %d one time passwords",
110+
}
50111

51112
// Used in the 2fa module
52-
TxtEmailVerifyTriggered = "An e-mail has been sent to confirm 2FA activation"
53-
TxtEmailVerifySubject = "Add 2FA to Account"
54-
TxtInvalid2FAVerificationToken = "Invalid 2FA email verification token"
55-
Txt2FAAuthorizationRequired = "You must first authorize adding 2fa by e-mail"
56-
TxtInvalid2FACode = "2FA code was invalid"
57-
TxtRepeated2FACode = "2FA code was previously used"
58-
TxtTOTP2FANotActive = "TOTP 2FA is not active"
59-
TxtSMSNumberRequired = "You must provide a phone number"
60-
TxtSMSWaitToResend = "Please wait a few moments before resending the SMS code"
113+
TxtEmailVerifyTriggered = LocalizationKey{
114+
ID: "EmailVerifyTriggered",
115+
Default: "An e-mail has been sent to confirm 2FA activation",
116+
}
117+
TxtEmailVerifySubject = LocalizationKey{
118+
ID: "EmailVerifySubject",
119+
Default: "Add 2FA to Account",
120+
}
121+
TxtInvalid2FAVerificationToken = LocalizationKey{
122+
ID: "Invalid2FAVerificationToken",
123+
Default: "Invalid 2FA email verification token",
124+
}
125+
Txt2FAAuthorizationRequired = LocalizationKey{
126+
ID: "2FAAuthorizationRequired",
127+
Default: "You must first authorize adding 2fa by e-mail",
128+
}
129+
TxtInvalid2FACode = LocalizationKey{
130+
ID: "Invalid2FACode",
131+
Default: "2FA code was invalid",
132+
}
133+
TxtRepeated2FACode = LocalizationKey{
134+
ID: "Repeated2FACode",
135+
Default: "2FA code was previously used",
136+
}
137+
TxtTOTP2FANotActive = LocalizationKey{
138+
ID: "TOTP2FANotActive",
139+
Default: "TOTP 2FA is not active",
140+
}
141+
TxtSMSNumberRequired = LocalizationKey{
142+
ID: "SMSNumberRequired",
143+
Default: "You must provide a phone number",
144+
}
145+
TxtSMSWaitToResend = LocalizationKey{
146+
ID: "SMSWaitToResend",
147+
Default: "Please wait a few moments before resending the SMS code",
148+
}
61149
)
150+
151+
// // Translation constants
152+
// const (
153+
// TxtSuccess = "success"
154+
//
155+
// // Used in the auth module
156+
// TxtInvalidCredentials = "Invalid Credentials"
157+
// TxtAuthFailed = "Please login"
158+
//
159+
// // Used in the register module
160+
// TxtUserAlreadyExists = "User already exists"
161+
// TxtRegisteredAndLoggedIn = "Account successfully created, you are now logged in"
162+
//
163+
// // Used in the confirm module
164+
// TxtConfirmYourAccount = "Please verify your account, an e-mail has been sent to you."
165+
// TxtAccountNotConfirmed = "Your account has not been confirmed, please check your e-mail."
166+
// TxtInvalidConfirmToken = "Your confirmation token is invalid."
167+
// TxtConfrimationSuccess = "You have successfully confirmed your account."
168+
// TxtConfirmEmailSubject = "Confirm New Account"
169+
//
170+
// // Used in the lock module
171+
// TxtLocked = "Your account has been locked, please contact the administrator."
172+
//
173+
// // Used in the logout module
174+
// TxtLoggedOut = "You have been logged out"
175+
//
176+
// // Used in the oauth2 module
177+
// TxtOAuth2LoginOK = "Logged in successfully with %s."
178+
// TxtOAuth2LoginNotOK = "%s login cancelled or failed"
179+
//
180+
// // Used in the recover module
181+
// TxtRecoverInitiateSuccessFlash = "An email has been sent to you with further instructions on how to reset your password."
182+
// TxtPasswordResetEmailSubject = "Password Reset"
183+
// TxtRecoverSuccessMsg = "Successfully updated password"
184+
// TxtRecoverAndLoginSuccessMsg = "Successfully updated password and logged in"
185+
//
186+
// // Used in the otp module
187+
// TxtTooManyOTPs = "You cannot have more than %d one time passwords"
188+
//
189+
// // Used in the 2fa module
190+
// TxtEmailVerifyTriggered = "An e-mail has been sent to confirm 2FA activation"
191+
// TxtEmailVerifySubject = "Add 2FA to Account"
192+
// TxtInvalid2FAVerificationToken = "Invalid 2FA email verification token"
193+
// Txt2FAAuthorizationRequired = "You must first authorize adding 2fa by e-mail"
194+
// TxtInvalid2FACode = "2FA code was invalid"
195+
// TxtRepeated2FACode = "2FA code was previously used"
196+
// TxtTOTP2FANotActive = "TOTP 2FA is not active"
197+
// TxtSMSNumberRequired = "You must provide a phone number"
198+
// TxtSMSWaitToResend = "Please wait a few moments before resending the SMS code"
199+
// )

logout/logout.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@ func (l *Logout) Init(ab *authboss.Authboss) error {
2222
l.Authboss = ab
2323

2424
var logoutRouteMethod func(string, http.Handler)
25-
switch l.Authboss.Config.Modules.LogoutMethod {
25+
switch l.Config.Modules.LogoutMethod {
2626
case "GET":
27-
logoutRouteMethod = l.Authboss.Config.Core.Router.Get
27+
logoutRouteMethod = l.Config.Core.Router.Get
2828
case "POST":
29-
logoutRouteMethod = l.Authboss.Config.Core.Router.Post
29+
logoutRouteMethod = l.Config.Core.Router.Post
3030
case "DELETE":
31-
logoutRouteMethod = l.Authboss.Config.Core.Router.Delete
31+
logoutRouteMethod = l.Config.Core.Router.Delete
3232
default:
33-
return errors.Errorf("logout wants to register a logout route but was given an invalid method: %s", l.Authboss.Config.Modules.LogoutMethod)
33+
return errors.Errorf("logout wants to register a logout route but was given an invalid method: %s", l.Config.Modules.LogoutMethod)
3434
}
3535

36-
logoutRouteMethod("/logout", l.Authboss.Core.ErrorHandler.Wrap(l.Logout))
36+
logoutRouteMethod("/logout", l.Core.ErrorHandler.Wrap(l.Logout))
3737

3838
return nil
3939
}
@@ -61,7 +61,7 @@ func (l *Logout) Logout(w http.ResponseWriter, r *http.Request) error {
6161
authboss.DelKnownSession(w)
6262
authboss.DelKnownCookie(w)
6363

64-
handled, err = l.Authboss.Events.FireAfter(authboss.EventLogout, w, r)
64+
handled, err = l.Events.FireAfter(authboss.EventLogout, w, r)
6565
if err != nil {
6666
return err
6767
} else if handled {
@@ -70,8 +70,8 @@ func (l *Logout) Logout(w http.ResponseWriter, r *http.Request) error {
7070

7171
ro := authboss.RedirectOptions{
7272
Code: http.StatusTemporaryRedirect,
73-
RedirectPath: l.Authboss.Paths.LogoutOK,
74-
Success: authboss.TxtLoggedOut,
73+
RedirectPath: l.Paths.LogoutOK,
74+
Success: l.Localizef(r.Context(), authboss.TxtLoggedOut),
7575
}
76-
return l.Authboss.Core.Redirector.Redirect(w, r, ro)
76+
return l.Core.Redirector.Redirect(w, r, ro)
7777
}

0 commit comments

Comments
 (0)