public void ProcessRequest(HttpContext context) { string json; try { var repo = new Repository(); API.LoginRequestParams requestParams = API.GetLoginRequestParams(GetRequestBody(context)); User user = repo.Login(requestParams.Username, requestParams.Password); if (user != null) { Token token = repo.GetOrCreateToken(user); // If authorizing via PM auth command, store the UserID for this auth token if (!string.IsNullOrEmpty(requestParams.SUID)) { repo.MatchUserToAuthToken(user, requestParams.SUID); repo.SaveChanges(); } List<FirstChannelVisit> channelsVisited = repo.GetFirstChannelVisits(user.UserID); // TODO: Merge this up foreach (var chan in channelsVisited) chan.DateVisitDisplay = chan.DateVisit.ToString(); json = JsonConvert.SerializeObject(new { success = true, User = PlainUser.FromModel(user), Token = PlainToken.FromModel(token), ChannelsVisited = channelsVisited }); } else { json = JsonConvert.SerializeObject(new { success = false, UserMessage = "Invalid login." }); } } catch (Exception ex) { json = JsonConvert.SerializeObject(new { success = false, error = "There was an exception: " + ex.Message }); } SetNoCaching(context); context.Response.ContentType = "text/json"; context.Response.Write(json); }
public void ProcessRequest(HttpContext context) { string json; try { var repo = new Repository(); API.RegisterRequestParams requestParams = API.GetRegisterRequestParams(GetRequestBody(context)); string username = requestParams.Username.Trim(); if (!Irception.IsValidUsername(username)) { json = JsonConvert.SerializeObject(new { success = false, UserMessage = "Usernames consist of a-z 0-9 _ -" }); } else if (!repo.UsernameAvailable(username)) { json = JsonConvert.SerializeObject(new { success = false, UserMessage = "Username taken." }); } else if (repo.InviteAccepted(requestParams.SUID)) { json = JsonConvert.SerializeObject(new { success = false, UserMessage = "Invite already accepted." }); } else { User user = repo.Register(username, requestParams.Password, requestParams.SUID); if (user == null) throw new Exception("Error during registration."); Token token = repo.GetOrCreateToken(user); List<FirstChannelVisit> channelsVisited = repo.GetFirstChannelVisits(user.UserID); json = JsonConvert.SerializeObject(new { success = true, User = PlainUser.FromModel(user), Token = PlainToken.FromModel(token), ChannelsVisited = channelsVisited }); } } catch (Exception ex) { json = JsonConvert.SerializeObject(new { success = false, error = "There was an exception: " + ex.Message }); } SetNoCaching(context); context.Response.ContentType = "text/json"; context.Response.Write(json); }