Skip to content

Commit

Permalink
adds simple logout link functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ivarprudnikov committed Apr 22, 2024
1 parent 91820aa commit 6e0a3c2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
14 changes: 14 additions & 0 deletions cypress/e2e/auth.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ describe('auth spec', () => {
cy.contains('footer', 'User: joe').should('be.visible')
cy.clearCookies()
})
it('can logout after logging in', () => {
cy.visit('/accounts/login')
cy.get('#username').type('joe')
cy.get('#password').type('joe')
cy.get('.btn-primary').click()
cy.contains('footer', 'User: joe').should('be.visible')

cy.contains('header a', 'Logout').should('be.visible')
cy.get('.logout-link').click()

cy.contains('footer', 'User: joe').should('not.exist')

cy.clearCookies()
})
it('creates account and can login', () => {
Cypress.Cookies.debug(true)
cy.visit('/accounts/new')
Expand Down
14 changes: 14 additions & 0 deletions routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func AddRoutes(
preReq := newAppMiddleware(sessions, users)
mux.Handle("GET /accounts/login", preReq(loginPageHandler(sessions)))
mux.Handle("POST /accounts/login", preReq(loginAccountHandler(sessions, users)))
mux.Handle("GET /accounts/logout", preReq(logoutAccountHandler(sessions)))
mux.Handle("GET /accounts/new", preReq(createAccountPageHandler(sessions)))
mux.Handle("POST /accounts", preReq(createAccountHandler(sessions, users)))
mux.Handle("GET /messages", preReq(hasAuth(listMsgHandler(sessions, messages))))
Expand Down Expand Up @@ -128,6 +129,19 @@ func loginAccountHandler(sessions *sessions.CookieStore, store storage.UserStore
}
}

func logoutAccountHandler(sessions *sessions.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
sess, _ := sessions.Get(r, SESS_COOKIE)
sess.Values[SESS_USER_KEY] = nil
err := sess.Save(r, w)
if err != nil {
sendError(r.Context(), sess, w, "failed to save session", err)
return
}
http.Redirect(w, r, "/", http.StatusSeeOther)
}
}

func createAccountPageHandler(sessions *sessions.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
sess, _ := sessions.Get(r, SESS_COOKIE)
Expand Down
1 change: 1 addition & 0 deletions web/nav.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
{{if .session.user }}
<li class="nav-item"><a href="/messages" class="nav-link">Messages</a></li>
<li class="nav-item"><a href="/messages/new" class="nav-link">Create new</a></li>
<li class="nav-item"><a href="/accounts/logout" class="nav-link logout-link">Logout</a></li>
{{else}}
<li class="nav-item"><a href="/accounts/login" class="nav-link">Login</a></li>
<li class="nav-item"><a href="/accounts/new" class="nav-link">Sign up</a></li>
Expand Down

0 comments on commit 6e0a3c2

Please sign in to comment.