batazor/go-auth

View on GitHub
middleware/auth.go

Summary

Maintainability
A
0 mins
Test Coverage
package middleware

import (
    "errors"
    "net/http"

    "github.com/micro-company/go-auth/models/session"
    "github.com/micro-company/go-auth/utils"
)

func CheckAuth(next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        w.Header().Set("Content-Type", "application/json")

        var Authorization = r.Header.Get("Authorization")
        if Authorization == "" {
            w.WriteHeader(http.StatusUnauthorized)
            utils.Error(w, errors.New(`"not auth"`))
            return
        }

        token, err := sessionModel.VerifyToken(Authorization)
        if err != nil {
            w.WriteHeader(http.StatusUnauthorized)
            utils.Error(w, errors.New(`"`+err.Error()+`"`))
            return
        }

        if token.Valid {
            next.ServeHTTP(w, r)
        } else {
            w.WriteHeader(http.StatusUnauthorized)
            utils.Error(w, errors.New(`"token invalid"`))
        }
        return
    })
}