support signing in with email address

This commit is contained in:
Ryan Stafford 2024-06-21 17:43:13 -04:00
parent 253469fe6d
commit 4bef03b8af
2 changed files with 15 additions and 17 deletions

View file

@ -892,7 +892,6 @@ func SignUpOrLogin(w http.ResponseWriter, r *http.Request, ps httprouter.Params)
return return
} }
var token string var token string
var username string
switch r.FormValue("submit") { switch r.FormValue("submit") {
case "log in": case "log in":
login := lemmy.Login{ login := lemmy.Login{
@ -916,7 +915,6 @@ func SignUpOrLogin(w http.ResponseWriter, r *http.Request, ps httprouter.Params)
} }
if resp.JWT.IsValid() { if resp.JWT.IsValid() {
token = resp.JWT.String() token = resp.JWT.String()
username = r.FormValue("username")
deleteCookie(w, state.Host, "ShowNSFW") deleteCookie(w, state.Host, "ShowNSFW")
} }
case "sign up": case "sign up":
@ -947,7 +945,6 @@ func SignUpOrLogin(w http.ResponseWriter, r *http.Request, ps httprouter.Params)
return return
} }
if resp.JWT.IsValid() { if resp.JWT.IsValid() {
username = r.FormValue("username")
token = resp.JWT.String() token = resp.JWT.String()
} else { } else {
var alert string var alert string
@ -965,16 +962,16 @@ func SignUpOrLogin(w http.ResponseWriter, r *http.Request, ps httprouter.Params)
} }
} }
if token != "" { if token != "" {
state.GetUser(username) state.Client.Token = token
if state.User == nil { state.GetSite()
return if myUser, ok := state.Site.MyUser.Value(); ok {
setCookie(w, state.Host, "jwt", token)
userid := strconv.FormatInt(myUser.LocalUserView.Person.ID, 10)
setCookie(w, state.Host, "user", myUser.LocalUserView.Person.Name+":"+userid)
setCookie(w, state.Host, "jwt", token)
r.URL.Path = "/" + state.Host
http.Redirect(w, r, r.URL.String(), http.StatusFound)
} }
setCookie(w, state.Host, "jwt", token)
userid := strconv.FormatInt(state.User.PersonView.Person.ID, 10)
setCookie(w, state.Host, "user", state.User.PersonView.Person.Name+":"+userid)
setCookie(w, state.Host, "jwt", token)
r.URL.Path = "/" + state.Host
http.Redirect(w, r, r.URL.String(), http.StatusFound)
return return
} }
} }
@ -1120,11 +1117,12 @@ func UserOp(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
Render(w, "login.html", state) Render(w, "login.html", state)
return return
} else if resp.JWT.IsValid() { } else if resp.JWT.IsValid() {
state.GetUser(r.FormValue("username")) state.Client.Token = resp.JWT.String()
if state.User != nil { state.GetSite()
if myuser, ok := state.Site.MyUser.Value(); ok {
setCookie(w, state.Host, "jwt", resp.JWT.String()) setCookie(w, state.Host, "jwt", resp.JWT.String())
userid := strconv.FormatInt(state.User.PersonView.Person.ID, 10) userid := strconv.FormatInt(myuser.LocalUserView.Person.ID, 10)
setCookie(w, state.Host, "user", state.User.PersonView.Person.Name+":"+userid) setCookie(w, state.Host, "user", myuser.LocalUserView.Person.Name+":"+userid)
} }
} }
case "create_community": case "create_community":

View file

@ -290,7 +290,7 @@ func (state *State) GetSite() {
if len(state.Site.Taglines) > 0 { if len(state.Site.Taglines) > 0 {
state.Tagline = state.Site.Taglines[rand.Intn(len(state.Site.Taglines))].Content state.Tagline = state.Site.Taglines[rand.Intn(len(state.Site.Taglines))].Content
} }
if !state.Site.MyUser.IsValid() { if !state.Site.MyUser.IsValid() || state.Session == nil {
return return
} }
for _, c := range state.Site.MyUser.ValueOrZero().Follows { for _, c := range state.Site.MyUser.ValueOrZero().Follows {