F4Ke
F4Ke

Reputation: 1751

Angular & Rails - post data from rails, get with angular?

On my angular app I can use sso to oauth with google, twitter etc. It call my rails method to create the authentification token. This works, now I want to send the oauth token from my rails server to the angular.

My actual schema :

1) Angular : sso connexion button, call rails server -> 
2) Rails : token creation ->
3) Rails : send to specific url in angular front, in POST request ->
4) Angular : Get token from specific url, and perform connexion

But I don't know how to send the data like this,

Anybody as an idea?

Upvotes: 0

Views: 85

Answers (1)

hanothan
hanothan

Reputation: 231

Is 1) and 4) different Angular clients?

If you want to create a token by asking the rails server, the rails server should just pass the token back as part of the response.

You can do this in this manner. This is an example of a rails route/controller action that finds a certain user after they login.

def find
    user = User.find_by(email: params[:email])

    if user && user.authenticate(params[:password])
        return render json: {
            token: user.generate_auth_token,
            userId:  user.id
        }, status: 201
    else
        return render json: { error: 'Invalid Username or Password' }, status: 400
    end
end

I find the user by email, and then authenticate the password input, and then return a json object that has a token and other data I needed in angular like the userId.

Upvotes: 1

Related Questions