MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / makeSessionFromNameAndEmail

Method makeSessionFromNameAndEmail

rest/session_api.go:162–205  ·  view source on GitHub ↗

MakeSessionFromUserAndEmail first attempts to find the user by username. If found, updates the users's email if different. If no match for username, attempts to find by the user by email. If not found, and createUserIfNeeded=true, creates a new user based on username, email.

(username, email string, createUserIfNeeded bool)

Source from the content-addressed store, hash-verified

160// email if different. If no match for username, attempts to find by the user by email.
161// If not found, and createUserIfNeeded=true, creates a new user based on username, email.
162func (h *handler) makeSessionFromNameAndEmail(username, email string, createUserIfNeeded bool) error {
163
164 // First attempt lookup by username and make a login session for her.
165 user, err := h.db.Authenticator(h.ctx()).GetUser(username)
166 if err != nil {
167 return err
168 }
169
170 // Attempt email updates/lookups if an email is provided.
171 if len(email) > 0 {
172 if user != nil {
173 // User found, check whether the email needs to be updated
174 // (e.g. user has changed email in external auth system)
175 if email != user.Email() {
176 if err = h.db.Authenticator(h.ctx()).UpdateUserEmail(user, email); err != nil {
177 // Failure to update email during session creation is non-critical, log and continue.
178 base.InfofCtx(h.ctx(), base.KeyAuth, "Unable to update email for user %s during session creation. Session will still be created. Error:%v,", base.UD(username), err)
179 }
180 }
181 } else {
182 // User not found by username. Attempt user lookup by email. This provides backward
183 // compatibility for users that were originally created with id = email
184 if user, err = h.db.Authenticator(h.ctx()).GetUserByEmail(email); err != nil {
185 return err
186 }
187 }
188 }
189
190 // Couldn't find existing user.
191 if user == nil {
192 if !createUserIfNeeded {
193 return base.HTTPErrorf(http.StatusUnauthorized, "No such user")
194 }
195
196 // Create a User with the given username, email address, and a random password.
197 // CAS mismatch indicates the user has been created by another request underneath us, can continue with session creation
198 user, err = h.db.Authenticator(h.ctx()).RegisterNewUser(username, email)
199 if err != nil && !base.IsCasMismatch(err) {
200 return err
201 }
202 }
203
204 return h.makeSession(user)
205}
206
207// ADMIN API: Generates a login session for a user and returns the session ID and cookie name.
208func (h *handler) createUserSession() error {

Callers 2

handleFacebookPOSTMethod · 0.95
handleGooglePOSTMethod · 0.95

Calls 12

ctxMethod · 0.95
makeSessionMethod · 0.95
InfofCtxFunction · 0.92
UDFunction · 0.92
HTTPErrorfFunction · 0.92
IsCasMismatchFunction · 0.92
GetUserMethod · 0.80
UpdateUserEmailMethod · 0.80
GetUserByEmailMethod · 0.80
RegisterNewUserMethod · 0.80
EmailMethod · 0.65
AuthenticatorMethod · 0.45

Tested by

no test coverage detected