notAuth:=[]string{"/api/user/new","/api/user/login"}//List of endpoints that doesn't require auth
requestPath:=r.URL.Path//current request path
//check if request does not need authentication, serve the request if it doesn't need it
for_,value:=rangenotAuth{
ifvalue==requestPath{
next.ServeHTTP(w,r)
return
}
}
response:=make(map[string]interface{})
tokenHeader:=r.Header.Get("Authorization")//Grab the token from the header
iftokenHeader==""{//Token is missing, returns with error code 403 Unauthorized
response=Message(false,"Missing auth token")
w.WriteHeader(http.StatusForbidden)
w.Header().Add("Content-Type","application/json")
Respond(w,response)
return
}
splitted:=strings.Split(tokenHeader," ")//The token normally comes in format `Bearer {token-body}`, we check if the retrieved token matched this requirement