2024-05-08 11:50:57 +12:00
# Google Tutorial
2023-10-26 11:08:25 +13:00
2024-05-08 11:50:57 +12:00
This is a quick tutorial on how to configure Google authentication.
2023-10-26 11:08:25 +13:00
First you'll need a registered application in [Google Cloud ](https://console.cloud.google.com/welcome ), in order to get your OAuth 2.0 Client credentials.
1. On the Cloud's console **Quick access** section select **APIs & Services** , then **Credentials**
2. Click on ** + CREATE CREDENTIALS** and from the dropdown select **OAuth client ID**
3. From the google developers console, we will need: `client_id` & `client_secret`
2024-07-12 07:14:03 +12:00
4. Enter your callback uri under **Authorized redirect URIs** . E.g. `http://localhost:4000/auth/user/google/callback` .
2023-10-26 11:08:25 +13:00
Next we configure our resource to use google credentials:
2024-03-28 14:39:53 +13:00
```elixir
2023-10-26 11:08:25 +13:00
defmodule MyApp.Accounts.User do
2024-03-28 14:39:53 +13:00
use Ash.Resource,
extensions: [AshAuthentication],
domain: MyApp.Accounts
2023-10-26 11:08:25 +13:00
attributes do
...
end
authentication do
strategies do
2024-06-12 08:42:09 +12:00
google do
2024-05-08 11:50:57 +12:00
client_id MyApp.Secrets
redirect_uri MyApp.Secrets
2024-06-12 08:42:09 +12:00
client_secret MyApp.Secrets
2023-10-26 11:08:25 +13:00
end
end
end
end
```
Please check the guide on how to properly configure your Secrets
Then we need to define an action that will handle the oauth2 flow, for the google case it is `:register_with_google` it will handle both cases for our resource, user registration & login.
2024-03-28 14:39:53 +13:00
```elixir
2023-10-26 11:08:25 +13:00
defmodule MyApp.Accounts.User do
2024-03-28 14:39:53 +13:00
use Ash.Resource,
extensions: [AshAuthentication],
domain: MyApp.Accounts
2023-10-26 11:08:25 +13:00
# ...
actions do
create :register_with_google do
argument :user_info, :map, allow_nil?: false
argument :oauth_tokens, :map, allow_nil?: false
upsert? true
2024-07-12 07:14:03 +12:00
upsert_identity :unique_email
2023-10-26 11:08:25 +13:00
change AshAuthentication.GenerateTokenChange
# Required if you have the `identity_resource` configuration enabled.
2024-05-08 11:50:57 +12:00
change AshAuthentication.Strategy.OAuth2.IdentityChange
2023-10-26 11:08:25 +13:00
2024-05-08 11:50:57 +12:00
change fn changeset, _ ->
2023-10-26 11:08:25 +13:00
user_info = Ash.Changeset.get_argument(changeset, :user_info)
2024-05-08 11:50:57 +12:00
Ash.Changeset.change_attributes(changeset, Map.take(user_info, ["email"]))
2023-10-26 11:08:25 +13:00
end
end
end
2024-05-08 11:50:57 +12:00
2023-10-26 11:08:25 +13:00
# ...
2024-05-08 11:50:57 +12:00
2023-10-26 11:08:25 +13:00
end
```
2024-07-12 07:14:03 +12:00
Ensure you set the `hashed_password` to `allow_nil?` if you are also using the password strategy.
```elixir
defmodule MyApp.Accounts.User do
# ...
attributes do
# ...
attribute :hashed_password, :string, allow_nil?: true, sensitive?: true
end
# ...
end
```
And generate and run migrations in that case.
```bash
mix ash.codegen make_hashed_password_nullable
mix ash.migrate
```