Example #1
0
        public JsonResult Login([FromBody] Login model)
        {
            try
            {
                model.Email = model.Email.ToLower();
                var users      = _database.GetCollection <ReadUser>("users").AsQueryable();
                var userExists = users.Any(u => u.Email == model.Email);

                if (!userExists)
                {
                    return(Json(new { isLoggedIn = false }.ToJson(jsonWriterSettings)));
                }

                var myUser  = users.Single(x => x.Email == model.Email);
                var isValid = AuthHelper.CheckPassword(model.Password, myUser.Password, myUser.Salt);

                if (!isValid)
                {
                    return(Json(new { isLoggedIn = false }.ToJson(jsonWriterSettings)));
                }
                else
                {
                    return(Json(new
                    {
                        isLoggedIn = true,
                        user =
                            new
                        {
                            FirstName = myUser.FirstName,
                            LastName = myUser.LastName,
                            Email = myUser.Email,
                            Id = myUser.Id
                        }
                    }.ToJson(jsonWriterSettings)));
                }
            }
            catch (Exception ex)
            {
                return(Json(new { Error = true, Message = ex }.ToJson(jsonWriterSettings)));
            }
        }