Rails Firebase Auth
Exemplo de autenticação de api rails com Firebase
Setup
-
Roda script de setup do rails
bin/setup
-
Copia o arquivo
.env.template
para.env
cp .env.template .env
-
Configura a variável de ambiente
FIREBASE_PROJECT_ID
no arquivo.env
-
Roda tarefa para gerar certificado do firebase
rails firebase:certificates:request
Rodando o projeto
rails s
Usando authenticação
Após o usuário criar conta ou autenticar com firebase no frontend web ou mobile, basta fazer as requisições para api passando o token do firebase.
Exemplo:
Get user profile
GET /profile
curl -X GET 'http://localhost:3001/profile' -H 'Authorization: Bearer user-token'
retorno:
{
"id": 1,
"name": "Wallas Faria da Silva",
"email": "user@email.com",
"phone": null,
"auth_id": "k5LWaAsas4544hhpl7ASIH9nm282",
"auth_provider": "google.com",
"created_at": "2021-01-25T00:51:41.055Z",
"updated_at": "2021-01-25T00:51:41.102Z",
"avatar_url": "http://localhost:3001/rails/active_storage/representations..."
}
Update user profile
PUT /profile
curl -X PUT 'http://localhost:3001/profile' \
-H 'Authorization: Bearer user-token' \
-H 'Content-Type: application/json' \
-d '{ "phone": "+5522556644998" }'
retorno:
{
"id": 1,
"phone": "+5522556644998",
"name": "Wallas Faria da Silva",
"email": "user@email.com",
"auth_id": "k5LWaAsas4544hhpl7ASIH9nm282",
"auth_provider": "google.com",
"created_at": "2021-01-25T00:51:41.055Z",
"updated_at": "2021-01-25T01:08:00.213Z",
"avatar_url": "http://localhost:3001/rails/active_storage/representations/..."
}