private async Task <string> RefreshSessionAsync(User user, Guid sessionId) { var tokens = tokenRepository.GetNewTokens(user); var currentSession = await sessionRepository.FindById(sessionId, user.User_Id); if (currentSession != null) { sessionRepository.RefreshSession(currentSession, tokens.Refresh_Token); } else { await sessionRepository.Insert(new Session { Session_Id = sessionId, User_Id = user.User_Id, RefreshToken = tokens.Refresh_Token }); } var result = new { access_token = tokens.Access_Token, refresh_token = tokens.Refresh_Token, user_id = user.User_Id, username = user.UserName, email = user.Email, avatar = user.Avatar }; return(snakeCaseConverter.ConvertToSnakeCase(result)); }
public async ValueTask <string> GetUserProfile(Guid userId) { var usr = _context.Users.Find(userId); var user = new { usr.User_Id, usr.UserName /*,usr.Avatar_Path*/, usr.Locale }; var subs = _context.Subscribes.Where(s => s.Publisher_Id == userId).Count(); var follows = _context.Subscribes.Where(s => s.Follower_Id == userId).Count(); var content = new { User = user, Subscribers = subs, Follows = follows }; return(snakeCaseConverter.ConvertToSnakeCase(content)); }
public async Task <IActionResult> FindAllAsync() { try { var content = await deckRepository.FindAll(); logger.LogInformation("------------> All decks have been returned <------------"); return(Ok(snakeCaseConverter.ConvertToSnakeCase(content))); } catch (Exception ex) { logger.LogWarning("------------> An error has occurred <------------ \n" + ex.Message); return(BadRequest(ex.Message)); } }
public async Task <string> Invoke(GetTokensParams getParams) { var user = userRepository.FindById(getParams.UserId); var session = sessionRepository.FindById(getParams.SessionId, getParams.UserId); var now = DateTime.UtcNow; var tokensPair = tokenRepository.GetNewTokens(await user); if (session == null) { sessionRepository.Insert(new Session { Session_Id = (await session).Session_Id, User_Id = (await user).User_Id, RefreshToken = tokensPair.Refresh_Token, }); } else { sessionRepository.RefreshSession(await session, tokensPair.Refresh_Token); } var result = new { access_token = tokensPair.Access_Token, refresh_token = tokensPair.Refresh_Token, user_Id = (await user).User_Id }; return(snakeCaseConverter.ConvertToSnakeCase(result)); }
public async Task <string> Invoke(SignInWithEmailParams param) { try { if (userRepository.IsEmailUnique(param.email)) { var user = await userRepository.FindByEmail(param.email); if (authOptions.validatePassword(param.password, user.Password)) { var tokens = tokenRepository.GetNewTokens(user); var currentSession = await sessionRepository.FindById(param.SessionId, user.User_Id); if (currentSession != null) { sessionRepository.RefreshSession(currentSession, tokens.Refresh_Token); } else { await sessionRepository.Insert(new Session { Session_Id = param.SessionId, User_Id = user.User_Id, RefreshToken = tokens.Refresh_Token }); } var result = new { access_token = tokens.Access_Token, refresh_token = tokens.Refresh_Token, user_id = user.User_Id, username = user.UserName, email = user.Email, avatar = user.Avatar }; return(snakeCaseConverter.ConvertToSnakeCase(result)); } else { throw new WrongPasswordException(); } } else { throw new UserNotFoundException(); } } catch { throw; } }
public async Task <string> Invoke(RefreshTokensParams param) { var tkns = param.Tokens; var sessionId = param.SessionId; if (tokenRepository.ValidateExpiredAccessToken(tkns.Access_Token)) { var handler = new JwtSecurityTokenHandler(); var tkn = handler.ReadJwtToken(tkns.Access_Token); var access_tkns_sample = new { id = "" }; var access_tkn_payload = JsonConvert.DeserializeAnonymousType(tkn.Payload.SerializeToJson(), access_tkns_sample); var userId = Guid.Parse(access_tkn_payload.id); var session = sessionRepository.FindById(sessionId, userId); if (await session == null) { throw new Exception("Non existent session"); } var usr = userRepository.FindById(userId); if (await usr == null) { throw new Exception("Non existent user"); } if (tkns.Refresh_Token == (await session).RefreshToken) { var tokens = tokenRepository.GetNewTokens(await usr); var result = new { access_token = tokens.Access_Token, refresh_token = tokens.Refresh_Token, user_Id = userId }; return(snakeCaseConverter.ConvertToSnakeCase(result)); } } throw new Exception(); }
public async Task <string> Invoke(SignUpWithEmailParams param) { var sessionId = param.SessionId; try { var convertedUsr = param.User; if (userRepository.ValidateUserUnique(convertedUsr)) { var usrGuid = Guid.NewGuid(); var guid = Guid.NewGuid(); fileRepository.Insert(new File() { File_Id = guid, Md5 = "", Path = "", Size = 12, Type = "" }); var user = new User() { User_Id = usrGuid, Avatar = guid, Role_Name = "User", UserName = convertedUsr.UserName, Email = convertedUsr.Email, Password = new AuthUtils().GetPasswordWithSalt(convertedUsr.Password), Tag = "Confirmed" }; await userRepository.Insert(user); /* var token = await tokenRepository.GetEmailConfirmationToken(usrGuid); * * await mailService.SendConfirmationEmail(convertedUsr.Email, token);*/ var tokens = tokenRepository.GetNewTokens(user); var currentSession = await sessionRepository.FindById(sessionId, user.User_Id); if (currentSession != null) { sessionRepository.RefreshSession(currentSession, tokens.Refresh_Token); } else { await sessionRepository.Insert(new Session { Session_Id = sessionId, User_Id = user.User_Id, RefreshToken = tokens.Refresh_Token }); } var result = new { access_token = tokens.Access_Token, refresh_token = tokens.Refresh_Token, user_id = user.User_Id, email = user.Email, username = user.UserName, avatar = user.Avatar }; return(snakeCaseConverter.ConvertToSnakeCase(result)); } else { throw new AlreadyUsedEmailException(); } } catch { throw; } }