Skip to content Skip to footer navigation

Oauth Tag

If you're using OAuth to manage user authentication, you may find you need to generate login URLs at some point. Here's how you do it.

Generating login URLs#

Here's the regular/parameter syntax in action, especially useful if the provider name comes from variable.

<a href="{{ oauth provider="github" }}">Sign In with Github</a>
<a href="{{ Statamic::tag('oauth')->provider('github') }}">Sign In with Github</a>
<a href="/oauth/github">Sign In with Github</a>

And the shorthand version.

<a href="{{ oauth:github }}">Sign In with Github</a>
<a href="{{ Statamic::tag('oauth:github') }}">Sign In with Github</a>
<a href="/oauth/github">Sign In with Github</a>

And now with a redirect:

<a href="{{ oauth:github redirect="/account" }}">Sign In with Github</a>
<a href="{{ Statamic::tag('oauth:github')->redirect('/account') }}">Sign In with Github</a>
<a href="/oauth/github?redirect=/account">Sign In with Github</a>

Looping through providers#

Use {{ oauth }} ... {{ /oauth }} as a tag pair to loop through the configured providers. This is useful for building your own connected-accounts UI, since each provider tells you whether the current user has connected it.

Providers configured as stateless are excluded from the loop, since they can't be used for connecting accounts.

<ul>
{{ oauth }}
<li>
{{ if connected }}
{{ oauth:disconnect_form :provider="name" }}
<button type="submit">Disconnect {{ label }}</button>
{{ /oauth:disconnect_form }}
{{ else }}
<a href="{{ url }}">Connect {{ label }}</a>
{{ /if }}
</li>
{{ /oauth }}
</ul>
<ul>
<s:oauth>
<li>
@if ($connected)
<s:oauth:disconnect_form :provider="$name">
<button type="submit">Disconnect {{ $label }}</button>
</s:oauth:disconnect_form>
@else
<a href="{{ $url }}">Connect {{ $label }}</a>
@endif
</li>
</s:oauth>
</ul>

Parameters

provider

string|tagpart

The provider to be used. You may either specify as a parameter or as a tagpart for shorthand: {{ oauth provider="github" }} or {{ oauth:github }}

redirect

string

The URL to be taken to after authenticating. This will be appending onto the generated URL as a query parameter.

Variables

Variable Type Description

name

string

The provider's handle, e.g. github.

label

string

The provider's display label, e.g. GitHub.

connected

boolean

Whether the current user has connected this provider to their account. false when nobody is logged in.

url

string

The provider's login URL. Doubles as the "connect" URL when the user is already logged in.