Skip to content

Commit

Permalink
Rename "login" terms to "sign in"
Browse files Browse the repository at this point in the history
  • Loading branch information
theandrew168 committed Oct 13, 2024
1 parent 5c477c9 commit 6dc39ad
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 87 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ make run

### OAuth Services

For authentication, this project relies on OAuth social logins (from GitHub and Google).
For authentication, this project relies on OAuth social sign ins (from GitHub and Google).
To work on the auth system, you'll need to create a `bloggulus.local.conf` file that contains the necessary OAuth credentials (client ID and client secret for each service).
If you need these credentials, feel free to reach out.

Expand All @@ -50,7 +50,7 @@ Then, you can run the app using the local config file with:
make run-local
```

Otherwise, you can simply run the application normally (without OAuth configured) and use the local-only debug login.
Otherwise, you can simply run the application normally (without OAuth configured) and use the local-only debug sign in.
This is enabled when `ENABLE_DEBUG_AUTH` is set (which the already Makefile includes for `run` and `run-local`).

### Testing
Expand Down
10 changes: 5 additions & 5 deletions backend/web/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,16 @@ func Handler(
enableDebugAuth := os.Getenv("ENABLE_DEBUG_AUTH") != ""

// Authenication routes.
mux.Handle("GET /signin", HandleLogin(enableDebugAuth))
mux.Handle("GET /github/signin", HandleOAuthLogin(&githubConf))
mux.Handle("GET /signin", HandleSignIn(enableDebugAuth))
mux.Handle("GET /github/signin", HandleOAuthSignIn(&githubConf))
mux.Handle("GET /github/callback", HandleOAuthCallback(&githubConf, repo, FetchGithubUserID))
mux.Handle("GET /google/signin", HandleOAuthLogin(&googleConf))
mux.Handle("GET /google/signin", HandleOAuthSignIn(&googleConf))
mux.Handle("GET /google/callback", HandleOAuthCallback(&googleConf, repo, FetchGoogleUserID))
mux.Handle("POST /logout", HandleLogoutForm(repo))
mux.Handle("POST /signout", HandleSignOutForm(repo))

// Debug-only auth routes.
if enableDebugAuth {
mux.Handle("POST /debug/signin", HandleDebugLogin(repo))
mux.Handle("POST /debug/signin", HandleDebugSignIn(repo))
}

// Public blog routes.
Expand Down
2 changes: 1 addition & 1 deletion backend/web/layout/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
{{if .Account}}
<li><a class="header__link" href="/blogs">Blogs</a></li>
<li>
<form action="/logout" method="post">
<form action="/signout" method="post">
<input type="hidden" name="csrf_token" value="{{$.CSRFToken}}">
<button class="header__link" type="submit">Sign out</button>
</form>
Expand Down
28 changes: 14 additions & 14 deletions backend/web/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ func FetchGoogleUserID(client *http.Client) (string, error) {
return username, nil
}

func HandleLogin(enableDebugAuth bool) http.Handler {
tmpl := page.NewLogin()
func HandleSignIn(enableDebugAuth bool) http.Handler {
tmpl := page.NewSignIn()
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Check for a "next" query param for post-auth redirecting.
next := r.URL.Query().Get("next")
Expand All @@ -113,7 +113,7 @@ func HandleLogin(enableDebugAuth bool) http.Handler {
cookie := util.NewSessionCookie(util.NextCookieName, next)
http.SetCookie(w, &cookie)

data := page.LoginData{
data := page.SignInData{
BaseData: util.TemplateBaseData(r, w),

EnableDebugAuth: enableDebugAuth,
Expand All @@ -124,7 +124,7 @@ func HandleLogin(enableDebugAuth bool) http.Handler {
})
}

func HandleOAuthLogin(conf *oauth2.Config) http.Handler {
func HandleOAuthSignIn(conf *oauth2.Config) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
state, err := random.BytesBase64(16)
if err != nil {
Expand All @@ -139,8 +139,8 @@ func HandleOAuthLogin(conf *oauth2.Config) http.Handler {
}

func HandleOAuthCallback(conf *oauth2.Config, repo *repository.Repository, fetchUserID FetchUserID) http.Handler {
// TODO: Replace the 400s with login page re-renders.
// tmpl := page.NewLogin()
// TODO: Replace the 400s with sign in page re-renders.
// tmpl := page.NewSignIn()
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Clear out the state expiredStateCookie.
expiredStateCookie := util.NewExpiredCookie(util.StateCookieName)
Expand Down Expand Up @@ -196,7 +196,7 @@ func HandleOAuthCallback(conf *oauth2.Config, repo *repository.Repository, fetch
return
}

slog.Info("register",
slog.Info("account created",
"account_id", account.ID(),
)
}
Expand All @@ -214,11 +214,11 @@ func HandleOAuthCallback(conf *oauth2.Config, repo *repository.Repository, fetch
return
}

// Set a permanent cookie after login.
// Set a permanent cookie after sign in.
sessionCookie := util.NewPermanentCookie(util.SessionCookieName, sessionID, util.SessionCookieTTL)
http.SetCookie(w, &sessionCookie)

slog.Info("login",
slog.Info("account signed in",
"account_id", account.ID(),
"session_id", session.ID(),
)
Expand All @@ -236,9 +236,9 @@ func HandleOAuthCallback(conf *oauth2.Config, repo *repository.Repository, fetch
})
}

func HandleDebugLogin(repo *repository.Repository) http.Handler {
func HandleDebugSignIn(repo *repository.Repository) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Generate a random userID for the debug login.
// Generate a random userID for the debug sign in.
userID, err := random.BytesBase64(16)
if err != nil {
util.InternalServerErrorResponse(w, r, err)
Expand Down Expand Up @@ -270,7 +270,7 @@ func HandleDebugLogin(repo *repository.Repository) http.Handler {
return
}

slog.Info("register",
slog.Info("account created",
"account_id", account.ID(),
)
}
Expand All @@ -288,11 +288,11 @@ func HandleDebugLogin(repo *repository.Repository) http.Handler {
return
}

// Set a permanent cookie after login.
// Set a permanent cookie after sign in.
sessionCookie := util.NewPermanentCookie(util.SessionCookieName, sessionID, util.SessionCookieTTL)
http.SetCookie(w, &sessionCookie)

slog.Info("login",
slog.Info("account signed in",
"account_id", account.ID(),
"session_id", session.ID(),
)
Expand Down
2 changes: 1 addition & 1 deletion backend/web/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/theandrew168/bloggulus/backend/web/util"
)

func HandleLogoutForm(repo *repository.Repository) http.Handler {
func HandleSignOutForm(repo *repository.Repository) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Check for a session ID. If there isn't one, just redirect back home.
sessionID, err := r.Cookie(util.SessionCookieName)
Expand Down
8 changes: 4 additions & 4 deletions backend/web/middleware/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/theandrew168/bloggulus/backend/web/util"
)

func loginRedirectURL(path string) *url.URL {
url, err := url.Parse("/login")
func signInRedirectURL(path string) *url.URL {
url, err := url.Parse("/signin")
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -65,7 +65,7 @@ func AccountRequired() Middleware {
// If the request context has no account, then the user is not logged in (redirect).
_, ok := util.GetContextAccount(r)
if !ok {
url := loginRedirectURL(r.URL.Path)
url := signInRedirectURL(r.URL.Path)
http.Redirect(w, r, url.String(), http.StatusSeeOther)
return
}
Expand All @@ -81,7 +81,7 @@ func AdminRequired() Middleware {
// If the request context has no account, then the user is not logged in (redirect).
account, ok := util.GetContextAccount(r)
if !ok {
url := loginRedirectURL(r.URL.Path)
url := signInRedirectURL(r.URL.Path)
http.Redirect(w, r, url.String(), http.StatusSeeOther)
return
}
Expand Down
10 changes: 5 additions & 5 deletions backend/web/middleware/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func TestAccountRequiredNoSession(t *testing.T) {

rr := w.Result()
test.AssertEqual(t, rr.StatusCode, http.StatusSeeOther)
test.AssertEqual(t, rr.Header.Get("Location"), "/login?next=%2F")
test.AssertEqual(t, rr.Header.Get("Location"), "/signin?next=%2F")
}

func TestAccountRequiredInvalidSession(t *testing.T) {
Expand All @@ -158,7 +158,7 @@ func TestAccountRequiredInvalidSession(t *testing.T) {

rr := w.Result()
test.AssertEqual(t, rr.StatusCode, http.StatusSeeOther)
test.AssertEqual(t, rr.Header.Get("Location"), "/login?next=%2F")
test.AssertEqual(t, rr.Header.Get("Location"), "/signin?next=%2F")
}

func TestAccountRequiredRedirect(t *testing.T) {
Expand All @@ -182,7 +182,7 @@ func TestAccountRequiredRedirect(t *testing.T) {

rr := w.Result()
test.AssertEqual(t, rr.StatusCode, http.StatusSeeOther)
test.AssertEqual(t, rr.Header.Get("Location"), "/login?next=%2Ffoobar")
test.AssertEqual(t, rr.Header.Get("Location"), "/signin?next=%2Ffoobar")
}

func TestAdminRequired(t *testing.T) {
Expand Down Expand Up @@ -242,7 +242,7 @@ func TestAdminRequiredNoSession(t *testing.T) {

rr := w.Result()
test.AssertEqual(t, rr.StatusCode, http.StatusSeeOther)
test.AssertEqual(t, rr.Header.Get("Location"), "/login?next=%2F")
test.AssertEqual(t, rr.Header.Get("Location"), "/signin?next=%2F")
}

func TestAdminRequiredInvalidSession(t *testing.T) {
Expand Down Expand Up @@ -270,7 +270,7 @@ func TestAdminRequiredInvalidSession(t *testing.T) {

rr := w.Result()
test.AssertEqual(t, rr.StatusCode, http.StatusSeeOther)
test.AssertEqual(t, rr.Header.Get("Location"), "/login?next=%2F")
test.AssertEqual(t, rr.Header.Get("Location"), "/signin?next=%2F")
}

func TestAdminRequiredNotAdmin(t *testing.T) {
Expand Down
40 changes: 0 additions & 40 deletions backend/web/page/login.html

This file was deleted.

16 changes: 8 additions & 8 deletions backend/web/page/login.go → backend/web/page/signin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
"golang.org/x/oauth2"
)

//go:embed login.html
var LoginHTML string
//go:embed signin.html
var SignInHTML string

type LoginData struct {
type SignInData struct {
layout.BaseData

GithubConf *oauth2.Config
Expand All @@ -21,23 +21,23 @@ type LoginData struct {
EnableDebugAuth bool
}

type LoginPage struct {
type SignInPage struct {
tmpl *template.Template
}

func NewLogin() *LoginPage {
func NewSignIn() *SignInPage {
sources := []string{
layout.BaseHTML,
LoginHTML,
SignInHTML,
}

tmpl := newTemplate("default", sources)
page := LoginPage{
page := SignInPage{
tmpl: tmpl,
}
return &page
}

func (p *LoginPage) Render(w io.Writer, data LoginData) error {
func (p *SignInPage) Render(w io.Writer, data SignInData) error {
return p.tmpl.ExecuteTemplate(w, "default", data)
}
40 changes: 40 additions & 0 deletions backend/web/page/signin.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{{define "main"}}

<section class="signin">
<article class="signin__card">
<h2 class="signin__heading">Welcome!</h2>

<hr />

{{if .EnableDebugAuth}}
<form method="POST" action="/debug/signin">
<input type="hidden" name="csrf_token" value="{{$.CSRFToken}}" />
<button class="signin__button" type="submit">
<img class="signin__icon" src="/img/bloggulus.png" />
Sign in with Debug
</button>
{{with .Errors.google}}
<p class="signin__error">{{.}}</p>
{{end}}
</form>
{{end}}

<a class="signin__button" href="/github/signin">
<img class="signin__icon" src="/img/github.png" />
Sign in with GitHub
</a>
{{with .Errors.github}}
<p class="signin__error">{{.}}</p>
{{end}}

<a class="signin__button" href="/google/signin">
<img class="signin__icon" src="/img/google.png" />
Sign in with Google
</a>
{{with .Errors.google}}
<p class="signin__error">{{.}}</p>
{{end}}
</article>
</section>

{{end}}
Loading

0 comments on commit 6dc39ad

Please sign in to comment.