feat: only show auth providers if they're enabled

This commit is contained in:
Sam 2023-04-18 23:31:57 +02:00
parent 17f2552c6a
commit f5d7bc4095
No known key found for this signature in database
GPG Key ID: B4EF20DDE721CAA1
4 changed files with 98 additions and 83 deletions

View File

@ -144,9 +144,9 @@ type oauthURLsRequest struct {
} }
type oauthURLsResponse struct { type oauthURLsResponse struct {
Discord string `json:"discord"` Discord string `json:"discord,omitempty"`
Tumblr string `json:"tumblr"` Tumblr string `json:"tumblr,omitempty"`
Google string `json:"google"` Google string `json:"google,omitempty"`
} }
func (s *Server) oauthURLs(w http.ResponseWriter, r *http.Request) error { func (s *Server) oauthURLs(w http.ResponseWriter, r *http.Request) error {
@ -162,22 +162,25 @@ func (s *Server) oauthURLs(w http.ResponseWriter, r *http.Request) error {
if err != nil { if err != nil {
return errors.Wrap(err, "setting CSRF state") return errors.Wrap(err, "setting CSRF state")
} }
var resp oauthURLsResponse
// copy Discord config and set redirect url if discordOAuthConfig.ClientID != "" {
discordCfg := discordOAuthConfig discordCfg := discordOAuthConfig
discordCfg.RedirectURL = req.CallbackDomain + "/auth/login/discord" discordCfg.RedirectURL = req.CallbackDomain + "/auth/login/discord"
// copy tumblr config resp.Discord = discordCfg.AuthCodeURL(state) + "&prompt=none"
}
if tumblrOAuthConfig.ClientID != "" {
tumblrCfg := tumblrOAuthConfig tumblrCfg := tumblrOAuthConfig
tumblrCfg.RedirectURL = req.CallbackDomain + "/auth/login/tumblr" tumblrCfg.RedirectURL = req.CallbackDomain + "/auth/login/tumblr"
// copy google config resp.Tumblr = tumblrCfg.AuthCodeURL(state)
}
if googleOAuthConfig.ClientID != "" {
googleCfg := googleOAuthConfig googleCfg := googleOAuthConfig
googleCfg.RedirectURL = req.CallbackDomain + "/auth/login/google" googleCfg.RedirectURL = req.CallbackDomain + "/auth/login/google"
resp.Google = googleCfg.AuthCodeURL(state)
}
render.JSON(w, r, oauthURLsResponse{ render.JSON(w, r, resp)
Discord: discordCfg.AuthCodeURL(state) + "&prompt=none",
Tumblr: tumblrCfg.AuthCodeURL(state),
Google: googleCfg.AuthCodeURL(state),
})
return nil return nil
} }

View File

@ -14,9 +14,9 @@ export interface MetaResponse {
} }
export interface UrlsResponse { export interface UrlsResponse {
discord: string; discord?: string;
tumblr: string; tumblr?: string;
google: string; google?: string;
} }
export interface ExportResponse { export interface ExportResponse {

View File

@ -60,9 +60,15 @@
<div class="col-md-4 mb-1"> <div class="col-md-4 mb-1">
<ListGroup> <ListGroup>
<ListGroupItem tag="button" on:click={toggleModal}>Log in with the Fediverse</ListGroupItem> <ListGroupItem tag="button" on:click={toggleModal}>Log in with the Fediverse</ListGroupItem>
{#if data.discord}
<ListGroupItem tag="a" href={data.discord}>Log in with Discord</ListGroupItem> <ListGroupItem tag="a" href={data.discord}>Log in with Discord</ListGroupItem>
{/if}
{#if data.tumblr}
<ListGroupItem tag="a" href={data.tumblr}>Log in with Tumblr</ListGroupItem> <ListGroupItem tag="a" href={data.tumblr}>Log in with Tumblr</ListGroupItem>
{/if}
{#if data.google}
<ListGroupItem tag="a" href={data.google}>Log in with Google</ListGroupItem> <ListGroupItem tag="a" href={data.google}>Log in with Google</ListGroupItem>
{/if}
</ListGroup> </ListGroup>
<Modal header="Pick an instance" isOpen={modalOpen} toggle={toggleModal}> <Modal header="Pick an instance" isOpen={modalOpen} toggle={toggleModal}>
<ModalBody> <ModalBody>

View File

@ -132,6 +132,7 @@
</CardBody> </CardBody>
</Card> </Card>
</div> </div>
{#if data.user.discord || data.urls.discord}
<div class="my-2"> <div class="my-2">
<Card> <Card>
<CardBody> <CardBody>
@ -148,12 +149,14 @@
<Button color="danger" disabled={!canUnlink} on:click={toggleDiscordUnlinkModal} <Button color="danger" disabled={!canUnlink} on:click={toggleDiscordUnlinkModal}
>Unlink account</Button >Unlink account</Button
> >
{:else} {:else if data.urls.discord}
<Button color="secondary" href={data.urls.discord}>Link account</Button> <Button color="secondary" href={data.urls.discord}>Link account</Button>
{/if} {/if}
</CardBody> </CardBody>
</Card> </Card>
</div> </div>
{/if}
{#if data.user.tumblr || data.urls.tumblr}
<div class="my-2"> <div class="my-2">
<Card> <Card>
<CardBody> <CardBody>
@ -170,12 +173,14 @@
<Button color="danger" disabled={!canUnlink} on:click={toggleTumblrUnlinkModal} <Button color="danger" disabled={!canUnlink} on:click={toggleTumblrUnlinkModal}
>Unlink account</Button >Unlink account</Button
> >
{:else} {:else if data.urls.tumblr}
<Button color="secondary" href={data.urls.tumblr}>Link account</Button> <Button color="secondary" href={data.urls.tumblr}>Link account</Button>
{/if} {/if}
</CardBody> </CardBody>
</Card> </Card>
</div> </div>
{/if}
{#if data.user.google || data.urls.google}
<div class="my-2"> <div class="my-2">
<Card> <Card>
<CardBody> <CardBody>
@ -192,12 +197,13 @@
<Button color="danger" disabled={!canUnlink} on:click={toggleGoogleUnlinkModal} <Button color="danger" disabled={!canUnlink} on:click={toggleGoogleUnlinkModal}
>Unlink account</Button >Unlink account</Button
> >
{:else} {:else if data.urls.google}
<Button color="secondary" href={data.urls.google}>Link account</Button> <Button color="secondary" href={data.urls.google}>Link account</Button>
{/if} {/if}
</CardBody> </CardBody>
</Card> </Card>
</div> </div>
{/if}
<Modal header="Pick an instance" isOpen={fediLinkModalOpen} toggle={toggleFediLinkModal}> <Modal header="Pick an instance" isOpen={fediLinkModalOpen} toggle={toggleFediLinkModal}>
<ModalBody> <ModalBody>
<Input placeholder="Instance (e.g. mastodon.social)" bind:value={instance} /> <Input placeholder="Instance (e.g. mastodon.social)" bind:value={instance} />