Enhance user registration and configuration handling
- Added support for processing environment variables in both register and form configurations, improving flexibility and configurability. - Introduced a new MessengerConfig structure for handling messenger-related settings in the register configuration. - Updated the RegisterConfig structure to include ThirdParty and InviteRequired fields, enhancing user registration options. - Refactored the registration endpoint to retrieve configuration details, ensuring a more robust registration process. - Implemented logging for missing environment variables to aid in configuration troubleshooting.
This commit is contained in:
parent
168313c757
commit
484ed8d899
4 changed files with 114 additions and 28 deletions
|
|
@ -396,6 +396,14 @@ func loadRegisterConfigs(_ string) error {
|
||||||
return fmt.Errorf("failed to parse register config %s: %v", filename, err)
|
return fmt.Errorf("failed to parse register config %s: %v", filename, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Process ENV variables in the configuration
|
||||||
|
processRegisterConfigENVVariables(&config)
|
||||||
|
|
||||||
|
// Copy third_party from signin config if available
|
||||||
|
if signinConfig, exists := fullConfigs[locale]; exists && signinConfig.ThirdParty != nil {
|
||||||
|
config.ThirdParty = signinConfig.ThirdParty
|
||||||
|
}
|
||||||
|
|
||||||
// Store register configuration
|
// Store register configuration
|
||||||
registerConfigs[locale] = &config
|
registerConfigs[locale] = &config
|
||||||
|
|
||||||
|
|
@ -614,6 +622,33 @@ func normalizeDuration(expiresIn string) (string, error) {
|
||||||
return normalized, nil
|
return normalized, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// processFormConfigENVVariables processes environment variables in the form configuration
|
||||||
|
func processFormConfigENVVariables(form *FormConfig) []string {
|
||||||
|
var missingEnvVars []string
|
||||||
|
|
||||||
|
if form == nil {
|
||||||
|
return missingEnvVars
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process form captcha options
|
||||||
|
if form.Captcha != nil && form.Captcha.Options != nil {
|
||||||
|
for key, value := range form.Captcha.Options {
|
||||||
|
if strValue, ok := value.(string); ok {
|
||||||
|
// Check if ENV variable exists before replacement
|
||||||
|
if strings.HasPrefix(strValue, "$ENV.") {
|
||||||
|
envVar := strings.TrimPrefix(strValue, "$ENV.")
|
||||||
|
if _, exists := os.LookupEnv(envVar); !exists {
|
||||||
|
missingEnvVars = append(missingEnvVars, envVar)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
form.Captcha.Options[key] = replaceENVVar(strValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return missingEnvVars
|
||||||
|
}
|
||||||
|
|
||||||
// processConfigENVVariables processes environment variables in the signin configuration
|
// processConfigENVVariables processes environment variables in the signin configuration
|
||||||
func processConfigENVVariables(config *Config) {
|
func processConfigENVVariables(config *Config) {
|
||||||
var missingEnvVars []string
|
var missingEnvVars []string
|
||||||
|
|
@ -635,24 +670,23 @@ func processConfigENVVariables(config *Config) {
|
||||||
}
|
}
|
||||||
config.ClientSecret = replaceENVVar(config.ClientSecret)
|
config.ClientSecret = replaceENVVar(config.ClientSecret)
|
||||||
|
|
||||||
// Process form captcha options
|
// Process form configuration
|
||||||
if config.Form != nil && config.Form.Captcha != nil && config.Form.Captcha.Options != nil {
|
formMissingVars := processFormConfigENVVariables(config.Form)
|
||||||
for key, value := range config.Form.Captcha.Options {
|
missingEnvVars = append(missingEnvVars, formMissingVars...)
|
||||||
if strValue, ok := value.(string); ok {
|
|
||||||
// Check if ENV variable exists before replacement
|
// Log warning for missing environment variables
|
||||||
if strings.HasPrefix(strValue, "$ENV.") {
|
if len(missingEnvVars) > 0 {
|
||||||
envVar := strings.TrimPrefix(strValue, "$ENV.")
|
fmt.Printf("Warning: The following environment variables are not set in signin configuration: %v\n", missingEnvVars)
|
||||||
if _, exists := os.LookupEnv(envVar); !exists {
|
|
||||||
missingEnvVars = append(missingEnvVars, envVar)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
config.Form.Captcha.Options[key] = replaceENVVar(strValue)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log warning for missing environment variables (optional, can be removed if log package not available)
|
// processRegisterConfigENVVariables processes environment variables in the register configuration
|
||||||
|
func processRegisterConfigENVVariables(config *RegisterConfig) {
|
||||||
|
// Process form configuration
|
||||||
|
missingEnvVars := processFormConfigENVVariables(config.Form)
|
||||||
|
|
||||||
|
// Log warning for missing environment variables
|
||||||
if len(missingEnvVars) > 0 {
|
if len(missingEnvVars) > 0 {
|
||||||
fmt.Printf("Warning: The following environment variables are not set in user configuration: %v\n", missingEnvVars)
|
fmt.Printf("Warning: The following environment variables are not set in register configuration: %v\n", missingEnvVars)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
41
openapi/user/register.go
Normal file
41
openapi/user/register.go
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
package user
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/yaoapp/yao/openapi/response"
|
||||||
|
"github.com/yaoapp/yao/openapi/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
// getRegisterConfig is the handler for get register configuration
|
||||||
|
func getRegisterConfig(c *gin.Context) {
|
||||||
|
// Get locale from query parameter (optional)
|
||||||
|
locale := c.Query("locale")
|
||||||
|
|
||||||
|
// Get register configuration for the specified locale (already includes third_party from signin config)
|
||||||
|
config := GetRegisterConfig(locale)
|
||||||
|
|
||||||
|
// Set session id if not exists
|
||||||
|
sid := utils.GetSessionID(c)
|
||||||
|
if sid == "" {
|
||||||
|
sid = generateSessionID()
|
||||||
|
response.SendSessionCookie(c, sid)
|
||||||
|
}
|
||||||
|
|
||||||
|
// If no configuration found, return error
|
||||||
|
if config == nil {
|
||||||
|
errorResp := &response.ErrorResponse{
|
||||||
|
Code: response.ErrInvalidRequest.Code,
|
||||||
|
ErrorDescription: "No register configuration found for the requested locale",
|
||||||
|
}
|
||||||
|
response.RespondWithError(c, response.StatusNotFound, errorResp)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return the register configuration
|
||||||
|
response.RespondWithSuccess(c, response.StatusOK, config)
|
||||||
|
}
|
||||||
|
|
||||||
|
// register is the handler for user registration
|
||||||
|
func register(c *gin.Context) {
|
||||||
|
// This is a placeholder - you may need to implement the actual registration logic here
|
||||||
|
}
|
||||||
|
|
@ -41,10 +41,12 @@ type Config struct {
|
||||||
type FormConfig struct {
|
type FormConfig struct {
|
||||||
Username *UsernameConfig `json:"username,omitempty"`
|
Username *UsernameConfig `json:"username,omitempty"`
|
||||||
Password *PasswordConfig `json:"password,omitempty"`
|
Password *PasswordConfig `json:"password,omitempty"`
|
||||||
|
ConfirmPassword *PasswordConfig `json:"confirm_password,omitempty"`
|
||||||
Captcha *CaptchaConfig `json:"captcha,omitempty"`
|
Captcha *CaptchaConfig `json:"captcha,omitempty"`
|
||||||
ForgotPasswordLink bool `json:"forgot_password_link,omitempty"`
|
ForgotPasswordLink bool `json:"forgot_password_link,omitempty"`
|
||||||
RememberMe bool `json:"remember_me,omitempty"`
|
RememberMe bool `json:"remember_me,omitempty"`
|
||||||
RegisterLink string `json:"register_link,omitempty"`
|
RegisterLink string `json:"register_link,omitempty"`
|
||||||
|
LoginLink string `json:"login_link,omitempty"`
|
||||||
TermsOfServiceLink string `json:"terms_of_service_link,omitempty"`
|
TermsOfServiceLink string `json:"terms_of_service_link,omitempty"`
|
||||||
PrivacyPolicyLink string `json:"privacy_policy_link,omitempty"`
|
PrivacyPolicyLink string `json:"privacy_policy_link,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
@ -93,7 +95,15 @@ type RegisterConfig struct {
|
||||||
Role string `json:"role,omitempty"`
|
Role string `json:"role,omitempty"`
|
||||||
Type string `json:"type,omitempty"` // User type id
|
Type string `json:"type,omitempty"` // User type id
|
||||||
Form *FormConfig `json:"form,omitempty"`
|
Form *FormConfig `json:"form,omitempty"`
|
||||||
ConfirmPassword *PasswordConfig `json:"confirm_password,omitempty"`
|
Messenger *MessengerConfig `json:"messenger,omitempty"`
|
||||||
|
InviteRequired bool `json:"invite_required,omitempty"`
|
||||||
|
ThirdParty *ThirdParty `json:"third_party,omitempty"` // Third party login configuration (copied from signin config)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MessengerConfig represents the messenger configuration for user registration
|
||||||
|
type MessengerConfig struct {
|
||||||
|
Channel string `json:"channel,omitempty"`
|
||||||
|
Templates map[string]string `json:"templates,omitempty"` // mail, sms templates
|
||||||
}
|
}
|
||||||
|
|
||||||
// YaoClientConfig represents the Yao OpenAPI Client config
|
// YaoClientConfig represents the Yao OpenAPI Client config
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,8 @@ func Attach(group *gin.RouterGroup, oauth types.OAuth) {
|
||||||
group.GET("/login", getLoginConfig) // Get login page config (public) - migrated from /signin
|
group.GET("/login", getLoginConfig) // Get login page config (public) - migrated from /signin
|
||||||
group.POST("/login", login) // User login (public) - migrated from /signin
|
group.POST("/login", login) // User login (public) - migrated from /signin
|
||||||
group.GET("/login/captcha", getCaptcha) // Get captcha for login (public)
|
group.GET("/login/captcha", getCaptcha) // Get captcha for login (public)
|
||||||
group.POST("/register", placeholder) // User register (public)
|
group.GET("/register", getRegisterConfig) // Get register page config (public)
|
||||||
|
group.POST("/register", register) // User register (public)
|
||||||
group.POST("/logout", oauth.Guard, placeholder) // User logout
|
group.POST("/logout", oauth.Guard, placeholder) // User logout
|
||||||
|
|
||||||
// Logined User Settings
|
// Logined User Settings
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue