github.com/ngocphuongnb/tetua@v0.0.7-alpha/app/web/user/active.go (about) 1 package webuser 2 3 import ( 4 "strconv" 5 "strings" 6 "time" 7 8 "github.com/ngocphuongnb/tetua/app/repositories" 9 "github.com/ngocphuongnb/tetua/app/server" 10 "github.com/ngocphuongnb/tetua/app/utils" 11 "github.com/ngocphuongnb/tetua/views" 12 ) 13 14 func Active(c server.Context) (err error) { 15 code := c.Query("code") 16 if code == "" { 17 return c.Redirect(utils.Url("")) 18 } 19 20 activation, err := utils.Decrypt(code) 21 if err != nil { 22 return c.Render(views.Message("Something went wrong", "Invalid activation code.", "", 0)) 23 } 24 25 parts := strings.Split(activation, "_") 26 if len(parts) != 2 { 27 return c.Render(views.Message("Something went wrong", "Invalid activation code.", "", 0)) 28 } 29 30 userID, err := strconv.Atoi(parts[0]) 31 if err != nil { 32 return c.Render(views.Message("Something went wrong", "Invalid activation code.", "", 0)) 33 } 34 35 exp, err := strconv.ParseInt(parts[1], 10, 64) 36 37 if err != nil { 38 return c.Render(views.Message("Something went wrong", "Invalid activation code.", "", 0)) 39 } 40 41 if time.Now().UnixMicro() > exp { 42 return c.Render(views.Message("Something went wrong", "Activation code has expired.", "", 0)) 43 } 44 45 user, err := repositories.User.ByID(c.Context(), userID) 46 if err != nil { 47 return c.Render(views.Message("Something went wrong", "Can't activate your account, please contact us for more information.", "", 0)) 48 } 49 if user.Active { 50 return c.Redirect(utils.Url("")) 51 } 52 user.Active = true 53 if _, err := repositories.User.Update(c.Context(), user); err != nil { 54 c.Logger().Error(err) 55 return c.Render(views.Message("Something went wrong", "Can't activate your account, please contact us for more information.", "", 0)) 56 } 57 58 return c.Render(views.Message("Success", "Your account has been activated, please login.", utils.Url(""), 5)) 59 }