Skip to content

Commit 286b1e5

Browse files
committed
Add a few signin API routes
1 parent 394b226 commit 286b1e5

6 files changed

Lines changed: 98 additions & 9 deletions

File tree

backend/query/article.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ import (
1414
// Count: all, all by account, search, search by account
1515

1616
type Article struct {
17-
Title string `db:"title"`
18-
URL string `db:"url"`
19-
BlogTitle string `db:"blog_title"`
20-
BlogURL string `db:"blog_url"`
21-
PublishedAt time.Time `db:"published_at"`
22-
Tags []string `db:"tags"`
17+
Title string `db:"title" json:"title"`
18+
URL string `db:"url" json:"url"`
19+
BlogTitle string `db:"blog_title" json:"blogTitle"`
20+
BlogURL string `db:"blog_url" json:"blogURL"`
21+
PublishedAt time.Time `db:"published_at" json:"publishedAt"`
22+
Tags []string `db:"tags" json:"tags"`
2323
}
2424

2525
func (qry *Query) ListRecentArticles(limit, offset int) ([]Article, error) {

backend/web/api/auth.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package api
2+
3+
import (
4+
"net/http"
5+
6+
"golang.org/x/oauth2"
7+
8+
"github.com/theandrew168/bloggulus/backend/random"
9+
"github.com/theandrew168/bloggulus/backend/web/api/jsonutil"
10+
)
11+
12+
func HandleOAuthSignin(conf *oauth2.Config) http.Handler {
13+
type response struct {
14+
State string `json:"state"`
15+
AuthCodeURL string `json:"authCodeURL"`
16+
}
17+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
18+
state, err := random.BytesBase64(16)
19+
if err != nil {
20+
panic(err)
21+
}
22+
23+
authCodeURL := conf.AuthCodeURL(state)
24+
25+
jsonutil.Write(w, http.StatusOK, response{
26+
State: state,
27+
AuthCodeURL: authCodeURL,
28+
})
29+
})
30+
}

backend/web/api/handler.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,40 @@
11
package api
22

33
import (
4-
"io/fs"
54
"net/http"
65

6+
"golang.org/x/oauth2"
7+
"golang.org/x/oauth2/github"
8+
"golang.org/x/oauth2/google"
9+
710
"github.com/theandrew168/bloggulus/backend/command"
11+
"github.com/theandrew168/bloggulus/backend/config"
812
"github.com/theandrew168/bloggulus/backend/query"
913
)
1014

1115
func Handler(
12-
public fs.FS,
16+
conf config.Config,
1317
cmd *command.Command,
1418
qry *query.Query,
1519
) http.Handler {
20+
githubConf := oauth2.Config{
21+
Endpoint: github.Endpoint,
22+
ClientID: conf.GithubClientID,
23+
ClientSecret: conf.GithubClientSecret,
24+
RedirectURL: conf.GithubRedirectURI,
25+
Scopes: []string{},
26+
}
27+
googleConf := oauth2.Config{
28+
Endpoint: google.Endpoint,
29+
ClientID: conf.GoogleClientID,
30+
ClientSecret: conf.GoogleClientSecret,
31+
RedirectURL: conf.GoogleRedirectURI,
32+
Scopes: []string{"https://www.googleapis.com/auth/userinfo.profile"},
33+
}
34+
1635
mux := http.NewServeMux()
1736
mux.Handle("GET /articles", HandleArticleList(qry))
37+
mux.Handle("GET /github/signin", HandleOAuthSignin(&githubConf))
38+
mux.Handle("GET /google/signin", HandleOAuthSignin(&googleConf))
1839
return mux
1940
}

backend/web/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func Handler(
9494
// The main application routes start here.
9595
mux.Handle("GET /{$}", HandleIndexPage(qry))
9696

97-
apiHandler := api.Handler(public, cmd, qry)
97+
apiHandler := api.Handler(conf, cmd, qry)
9898
mux.Handle("GET /api/v1/", http.StripPrefix("/api/v1", apiHandler))
9999

100100
// Check if the debug auth method should be enabled.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type { PageServerLoad } from "./$types";
2+
3+
type Article = {
4+
title: string;
5+
url: string;
6+
blogTitle: string;
7+
blogURL: string;
8+
publishedAt: string;
9+
tags: string[];
10+
};
11+
12+
type ArticlesResponse = {
13+
articles: Article[];
14+
};
15+
16+
export const load: PageServerLoad = async () => {
17+
const articlesResp = await fetch("http://localhost:5000/api/v1/articles");
18+
const articles: ArticlesResponse = await articlesResp.json();
19+
return { articles: articles.articles };
20+
};

frontend/src/routes/+page.svelte

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,20 @@
1+
<script lang="ts">
2+
import type { PageProps } from "./$types";
3+
4+
let { data }: PageProps = $props();
5+
</script>
6+
17
<h1>Welcome to SvelteKit</h1>
28
<p>Visit <a href="https://svelte.dev/docs/kit">svelte.dev/docs/kit</a> to read the documentation</p>
9+
10+
<a href="/signin/debug">Sign In (Debug)</a>
11+
<a href="/signin/github">Sign In (GitHub)</a>
12+
<a href="/signin/google">Sign In (Google)</a>
13+
14+
<ul>
15+
{#each data.articles as article}
16+
<li>
17+
{article.title}
18+
</li>
19+
{/each}
20+
</ul>

0 commit comments

Comments
 (0)