Skip to content

Instantly share code, notes, and snippets.

@NicholusMuwonge
Created October 24, 2020 15:27
Show Gist options
  • Save NicholusMuwonge/8e822ceb557e2c092976506643fb908f to your computer and use it in GitHub Desktop.
Save NicholusMuwonge/8e822ceb557e2c092976506643fb908f to your computer and use it in GitHub Desktop.
# Api Controller to handle our call backs
require 'json'
class Api::V1::SocialAuthController < ApplicationController
def authenticate_social_auth_user
# params is the response I receive from the client with the data from the provider about the user
@user = User.signin_or_create_from_provider(params) # this method add a user who is new or logins an old one
if @user.persisted?
# I log the user in at this point
sign_in(@user)
# after user is loggedIn, I generate a new_token here
login_token = @user.create_new_auth_token
render json: {
status: 'SUCCESS',
message: "user was successfully logged in through #{params[:provider]}",
headers: login_token
},
status: :created
else
render json: {
status: 'FAILURE',
message: "There was a problem signing you in through #{params[:provider]}",
data: @user.errors
},
status: :unprocessable_entity
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment