public HttpResponseMessage GetUserInfoByIdToken()
        {
            // Receive google id token from the client
            string idToken = Request.Headers.GetValues("idToken").FirstOrDefault();

            if (idToken == null)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }

            GetUserByIdTokenRequest request = new GetUserByIdTokenRequest()
            {
                IdToken = idToken
            };
            var responseModel = _businessLogic.GetUserByIdToken(request);

            // Response to client, Not Found if no user are found, otherwise OK
            if (responseModel == null)
            {
                return(Request.CreateResponse(HttpStatusCode.NotFound));
            }
            var response = Request.CreateResponse(HttpStatusCode.OK);

            response.Content = new StringContent(JsonConvert.SerializeObject(responseModel), Encoding.UTF8, "application/json");

            return(response);
        }
Пример #2
0
        public GetUserByIdTokenResponse GetUserByIdToken(GetUserByIdTokenRequest request)
        {
            GetUserByIdTokenResponse response = null;
            string idToken       = request.IdToken;
            string verifiedEmail = GoogleApiTokenVerifier.VerifyIdToken(idToken);

            if (verifiedEmail != null)
            {
                var student = _context.Students.Where(s => s.Email == verifiedEmail).SingleOrDefault();
                if (student != null)
                {
                    response = new GetUserByIdTokenResponse()
                    {
                        UserId    = student.StudentId,
                        FullName  = student.FullName,
                        UserEmail = student.Email,
                        UserRole  = "student"
                    };
                }
                else
                {
                    var teacher = _context.Teachers.Where(t => t.Email == verifiedEmail).SingleOrDefault();
                    if (teacher != null)
                    {
                        response = new GetUserByIdTokenResponse()
                        {
                            UserId    = teacher.TeacherId,
                            FullName  = teacher.FullName,
                            UserEmail = teacher.Email,
                            UserRole  = "teacher"
                        };
                    }
                }
            }
            return(response);
        }