Example code using Google auth with Axum

2022-08-02 av Sigurd Gartmann

On the path to converting a few old Javascript projects to Rust, I wanted to use Google for authentication. One of the Axum examples is using Oauth through Discord for authentication, and I used that as a base for this Google example.

There just a few changes needed, thanks to the Oauth2 and OIDC standards and the pretty clean example code.

In the Google example we pick some of the fields from the User object returned by Google:

struct User {
    sub: String, // The ID
    picture: Option<String>,
    email: String,
    name: String,
}

The scopes are also different:

        .add_scope(Scope::new("profile".to_string()))
        .add_scope(Scope::new("email".to_string()))

The rest is mainly changes to URLs, both Google's auth, token and userinfo URLs, and using /auth/google instead of /auth/discord internally.

The full example is stored at along with the diff of the changes needed to make it work.