// LogIn handles user's credentials checking and new session creation.
// On successful execution, a session will be created.
LogIn handles user's credentials checking and new session creation.
On successful execution, a session will be created.
// LogIn handles user ' s credentials checking and new session creation . // On successful execution a session will be created .
func (h *Handler) LogIn(w http.ResponseWriter, r *http.Request) {
	var cInp CoreInput
	if err := httpflow.DecodeJSON(r, &cInp); err != nil {
		httpflow.RespondError(h.log, w, r, err)
		return
	}
	if err := CheckEmail(cInp.Email); err != nil {
		httpflow.RespondError(h.log, w, r, ErrInvalidCredentials)
		return
	}
	ctx := r.Context()
	usr, err := h.db.FetchUserByEmail(ctx, cInp.Email)
	if err != nil {
		if errors.Is(err, httpflow.ErrNotFound) {
			err = ErrInvalidCredentials
		}
		httpflow.RespondError(h.log, w, r, err)
		return
	}
	if err = h.ext.loginCheck(ctx, usr); err != nil {
		httpflow.RespondError(h.log, w, r, err)
		return
	}
	usrC := usr.ExposeCore()
	if !usrC.IsPasswordCorrect(cInp.Password) {
		httpflow.RespondError(h.log, w, r, ErrInvalidCredentials)
		return
	}
	usrC.Recovery.Clear()
	if err = h.db.UpdateUser(ctx, usr); err != nil {
		httpflow.RespondError(h.log, w, r, err)
		return
	}
	sm := h.session.manager
	if cInp.RememberMe {
		sm = sm.Clone(sessionup.ExpiresIn(h.session.duration))
	}
	if err = sm.Init(w, r, usrC.ID.String()); err != nil {
		httpflow.RespondError(h.log, w, r, err)
		return
	}
	httpflow.Respond(h.log, w, r, usr, http.StatusOK)
}
func ( h * Handler ) LogIn ( w http . ResponseWriter , r * http . Request ) { var cInp CoreInput if err := httpflow . DecodeJSON ( r , & cInp ) ; err != nil { httpflow . RespondError ( h . log , w , r , err ) return } if err := CheckEmail ( cInp . Email ) ; err != nil { httpflow . RespondError ( h . log , w , r , ErrInvalidCredentials ) return } ctx := r . Context ( ) usr , err := h . db . FetchUserByEmail ( ctx , cInp . Email ) if err != nil { if errors . Is ( err , httpflow . ErrNotFound ) { err = ErrInvalidCredentials } httpflow . RespondError ( h . log , w , r , err ) return } if err = h . ext . loginCheck ( ctx , usr ) ; err != nil { httpflow . RespondError ( h . log , w , r , err ) return } usrC := usr . ExposeCore ( ) if ! usrC . IsPasswordCorrect ( cInp . Password ) { httpflow . RespondError ( h . log , w , r , ErrInvalidCredentials ) return } usrC . Recovery . Clear ( ) if err = h . db . UpdateUser ( ctx , usr ) ; err != nil { httpflow . RespondError ( h . log , w , r , err ) return } sm := h . session . manager if cInp . RememberMe { sm = sm . Clone ( sessionup . ExpiresIn ( h . session . duration ) ) } if err = sm . Init ( w , r , usrC . ID . String ( ) ) ; err != nil { httpflow . RespondError ( h . log , w , r , err ) return } httpflow . Respond ( h . log , w , r , usr , http . StatusOK ) }
LogIn handles user's credentials checking and new session creation.
LogIn handles user ' s credentials checking and new session creation .