ch17.1
This commit is contained in:
@@ -68,3 +68,13 @@ func (app *application) invalidAuthenticationTokenResponse(w http.ResponseWriter
|
|||||||
message := "invalid or missing authentication token"
|
message := "invalid or missing authentication token"
|
||||||
app.errorResponse(w, r, http.StatusUnauthorized, message)
|
app.errorResponse(w, r, http.StatusUnauthorized, message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (app *application) authenticationRequiredResponse(w http.ResponseWriter, r *http.Request) {
|
||||||
|
message := "you must be authenticated to access this resource"
|
||||||
|
app.errorResponse(w, r, http.StatusUnauthorized, message)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (app *application) inactiveAccountResponse(w http.ResponseWriter, r *http.Request) {
|
||||||
|
message := "your user account must be activated to access this resource"
|
||||||
|
app.errorResponse(w, r, http.StatusForbidden, message)
|
||||||
|
}
|
||||||
|
|||||||
@@ -123,3 +123,30 @@ func (app *application) authenticate(next http.Handler) http.Handler {
|
|||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (app *application) requireAuthenticatedUser(next http.HandlerFunc) http.HandlerFunc {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
user := app.contextGetUser(r)
|
||||||
|
|
||||||
|
if user.IsAnonymous() {
|
||||||
|
app.authenticationRequiredResponse(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (app *application) requireActivatedUser(next http.HandlerFunc) http.HandlerFunc {
|
||||||
|
fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
user := app.contextGetUser(r)
|
||||||
|
|
||||||
|
if !user.Activated {
|
||||||
|
app.inactiveAccountResponse(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
|
return app.requireAuthenticatedUser(fn)
|
||||||
|
}
|
||||||
|
|||||||
@@ -14,11 +14,11 @@ func (app *application) routes() http.Handler {
|
|||||||
|
|
||||||
router.HandlerFunc(http.MethodGet, "/v1/healthcheck", app.healthcheckHandler)
|
router.HandlerFunc(http.MethodGet, "/v1/healthcheck", app.healthcheckHandler)
|
||||||
|
|
||||||
router.HandlerFunc(http.MethodGet, "/v1/movies", app.listMoviesHandler)
|
router.HandlerFunc(http.MethodGet, "/v1/movies", app.requireActivatedUser(app.listMoviesHandler))
|
||||||
router.HandlerFunc(http.MethodPost, "/v1/movies", app.createMovieHandler)
|
router.HandlerFunc(http.MethodPost, "/v1/movies", app.requireActivatedUser(app.createMovieHandler))
|
||||||
router.HandlerFunc(http.MethodGet, "/v1/movies/:id", app.showMovieHandler)
|
router.HandlerFunc(http.MethodGet, "/v1/movies/:id", app.requireActivatedUser(app.showMovieHandler))
|
||||||
router.HandlerFunc(http.MethodPatch, "/v1/movies/:id", app.updateMovieHandler)
|
router.HandlerFunc(http.MethodPatch, "/v1/movies/:id", app.requireActivatedUser(app.updateMovieHandler))
|
||||||
router.HandlerFunc(http.MethodDelete, "/v1/movies/:id", app.deleteMovieHandler)
|
router.HandlerFunc(http.MethodDelete, "/v1/movies/:id", app.requireActivatedUser(app.deleteMovieHandler))
|
||||||
|
|
||||||
router.HandlerFunc(http.MethodPost, "/v1/users", app.registerUserHandler)
|
router.HandlerFunc(http.MethodPost, "/v1/users", app.registerUserHandler)
|
||||||
router.HandlerFunc(http.MethodPut, "/v1/users/activated", app.activateUserHandler)
|
router.HandlerFunc(http.MethodPut, "/v1/users/activated", app.activateUserHandler)
|
||||||
|
|||||||
Reference in New Issue
Block a user