public async Task <IActionResult> RegisterStudent(StudentDTO st)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(new { message = "Fill all fields" }));
            }
            var student = new Student()
            {
                FirstName = st.FirstName,
                LastName  = st.LastName,
                Email     = st.Email,
                Role      = "user",
            };

            if (await _authRepos.UserExists(student))
            {
                return(BadRequest(new { message = "This user already exists" }));
            }
            student = await _authRepos.Register(student, st.Password);

            if (student == null)
            {
                return(BadRequest(new { message = "Oops, failed to register" }));
            }
            var identity = await CreateIdentity(st.Email, st.Password);

            string token = TokenCreatingService.CreateToken(identity, out string identityName, Configuration);

            HttpContext.Session.SetString("Token", token);
            HttpContext.Session.SetInt32("Id", student.StudentId);
            HttpContext.Session.SetString("Name", student.FirstName);
            HttpContext.Session.SetString("Role", student.Role);
            return(Ok(new { token = token, name = identityName }));
        }
Esempio n. 2
0
        public async Task <IActionResult> Register(TRegisterUser regUser)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Fill all fields"));
            }
            var user = _mapper.Map <TUser>(regUser);

            try
            {
                await _userService.Register(user, regUser.Password);

                var identity = TokenCreatingService <TUser, TId> .CreateIdentity(user);

                var token = TokenCreatingService <TUser, TId> .CreateToken(identity, _configuration);

                return(Ok(token));
            }
            catch (UserFoundException e)
            {
                return(BadRequest(e.Message));
            }
            catch (EntityNotFoundException e)
            {
                return(BadRequest(e.Message));
            }
            catch (Exception)
            {
                return(this.ServerError("Failed to register"));
            }
        }
Esempio n. 3
0
        public async Task <IActionResult> Login(LoginDto authUser)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Fill all fields"));
            }
            var user = await _userService.Login(authUser.Email, authUser.Password);

            if (user == null)
            {
                return(Unauthorized("Wrong email or password"));
            }
            var identity = TokenCreatingService <TUser, TId> .CreateIdentity(user);

            string token = TokenCreatingService <TUser, TId> .CreateToken(identity, _configuration);

            return(Ok(token));
        }
Esempio n. 4
0
        public async Task <IActionResult> RegisterEmployer(EmployerDTO em)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(new { message = "Fill all fields" }));
            }
            var employer = new Employer()
            {
                FirstName   = em.FirstName,
                LastName    = em.LastName,
                Email       = em.Email,
                Role        = "user",
                CompanyName = em.CompanyName
            };
            var company = await _companyRepos.GetCompanyByName(em.CompanyName);

            if (company == null)
            {
                return(BadRequest(new { message = "Such company doesn't exist" }));
            }
            if (await _authRepos.UserExists(employer))
            {
                return(BadRequest(new { message = "This user already exists" }));
            }
            employer.CompanyId   = company.CompanyId;
            employer.CompanyName = company.Name;
            employer             = await _authRepos.Register(employer, em.Password);

            if (employer == null)
            {
                return(BadRequest(new { message = "Failed to register" }));
            }
            var identity = await CreateEmployerIdentity(em.Email, em.Password);

            string token = TokenCreatingService.CreateToken(identity, out string identityName, Configuration);

            HttpContext.Session.SetString("Token", token);
            HttpContext.Session.SetInt32("Id", employer.EmployerId);
            HttpContext.Session.SetString("Name", employer.FirstName);
            HttpContext.Session.SetString("Company", employer.CompanyName);
            return(Ok(new { token = token, name = identityName }));
        }
        public async Task <IActionResult> LoginStudent(StudentLoginDTO st)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(new { message = "Fill all fields" }));
            }
            var student = await CreateIdentity(st.Email, st.Password);

            if (student == null)
            {
                return(Unauthorized(new { message = "Username or password is incorrect" }));
            }
            string token = TokenCreatingService.CreateToken(student, out string identityName, Configuration);

            HttpContext.Session.SetString("Token", token);
            HttpContext.Session.SetInt32("Id", int.Parse(student.Claims.SingleOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value));
            HttpContext.Session.SetString("Name", student.Claims.SingleOrDefault(c => c.Type == ClaimTypes.Name).Value);
            HttpContext.Session.SetString("Role", student.Claims.SingleOrDefault(c => c.Type == ClaimTypes.Role).Value);
            return(Ok(new { token = token, name = identityName }));
        }
Esempio n. 6
0
        public async Task <IActionResult> LoginEmployer(EmployerLoginDTO em)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(new { message = "Fill all fields" }));
            }
            var employer = await CreateEmployerIdentity(em.Email, em.Password);

            if (employer == null)
            {
                return(Unauthorized(new { message = "Username or password is incorrect" }));
            }
            string token = TokenCreatingService.CreateToken(employer, out string identityName, Configuration);

            HttpContext.Session.SetString("Token", token);
            HttpContext.Session.SetInt32("Id", int.Parse(employer.Claims.SingleOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value));
            HttpContext.Session.SetString("Name", employer.Claims.SingleOrDefault(c => c.Type == ClaimTypes.Name).Value);
            HttpContext.Session.SetString("Company", employer.Claims.SingleOrDefault(c => c.Type == ClaimTypes.UserData).Value);
            return(Ok(new { token = token, name = identityName }));
        }