GetUser retrieves the user that is associated with the current session.
(w http.ResponseWriter, r *http.Request)
| 140 | |
| 141 | // GetUser retrieves the user that is associated with the current session. |
| 142 | func (s *Session) GetUser(w http.ResponseWriter, r *http.Request) (*http.Request, *ttnpb.User, error) { |
| 143 | if user, ok := r.Context().Value(userKey).(*ttnpb.User); ok { |
| 144 | return r, user, nil |
| 145 | } |
| 146 | r, session, err := s.Get(w, r) |
| 147 | if err != nil { |
| 148 | return r, nil, err |
| 149 | } |
| 150 | ctx := r.Context() |
| 151 | var user *ttnpb.User |
| 152 | err = s.Store.Transact(ctx, func(ctx context.Context, st Store) (err error) { |
| 153 | user, err = st.GetUser( |
| 154 | ctx, |
| 155 | &ttnpb.UserIdentifiers{UserId: session.GetUserIds().GetUserId()}, |
| 156 | nil, |
| 157 | ) |
| 158 | return err |
| 159 | }) |
| 160 | if err != nil { |
| 161 | return r, nil, err |
| 162 | } |
| 163 | return r.WithContext(context.WithValue(ctx, userKey, user)), user, nil |
| 164 | } |
| 165 | |
| 166 | // DoLogin performs the authentication using user id and password. |
| 167 | func (s *Session) DoLogin(ctx context.Context, userID, password string) error { |