-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* demo: auth * rename * rename
- Loading branch information
Showing
6 changed files
with
201 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package controllers | ||
|
||
import ( | ||
"github.com/goravel/framework/contracts/http" | ||
"github.com/goravel/framework/facades" | ||
|
||
"goravel/app/models" | ||
) | ||
|
||
type AuthController struct { | ||
// Dependent services | ||
} | ||
|
||
func NewAuthController() *AuthController { | ||
return &AuthController{ | ||
// Inject services | ||
} | ||
} | ||
|
||
func (r *AuthController) Login(ctx http.Context) http.Response { | ||
// Create a user | ||
var user models.User | ||
if err := facades.Orm().Query().FirstOrCreate(&user, models.User{ | ||
Name: ctx.Request().Input("name", "Goravel"), | ||
}); err != nil { | ||
return ctx.Response().Json(http.StatusInternalServerError, http.Json{ | ||
"error": err.Error(), | ||
}) | ||
} | ||
|
||
var ( | ||
token string | ||
err error | ||
) | ||
|
||
// Use different guards to login | ||
if guard := ctx.Request().Header("Guard"); guard == "" { | ||
token, err = facades.Auth(ctx).LoginUsingID(user.ID) | ||
if err != nil { | ||
return ctx.Response().String(http.StatusInternalServerError, err.Error()) | ||
} | ||
} else { | ||
token, err = facades.Auth(ctx).Guard(guard).Login(user) | ||
if err != nil { | ||
return ctx.Response().String(http.StatusInternalServerError, err.Error()) | ||
} | ||
} | ||
|
||
return ctx.Response().Header("Authorization", "Bearer "+token).Success().Json(http.Json{ | ||
"user": user, | ||
}) | ||
} | ||
|
||
func (r *AuthController) Info(ctx http.Context) http.Response { | ||
var user models.User | ||
|
||
if guard := ctx.Request().Header("Guard"); guard == "" { | ||
if err := facades.Auth(ctx).User(&user); err != nil { | ||
return ctx.Response().String(http.StatusInternalServerError, err.Error()) | ||
} | ||
} else { | ||
if err := facades.Auth(ctx).Guard(guard).User(&user); err != nil { | ||
return ctx.Response().String(http.StatusInternalServerError, err.Error()) | ||
} | ||
} | ||
|
||
return ctx.Response().Success().Json(http.Json{ | ||
"user": user, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package middleware | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/goravel/framework/auth" | ||
"github.com/goravel/framework/contracts/http" | ||
"github.com/goravel/framework/facades" | ||
) | ||
|
||
func Auth() http.Middleware { | ||
return func(ctx http.Context) { | ||
guard := facades.Config().GetString("auth.defaults.guard") | ||
if ctx.Request().Header("Guard") != "" { | ||
guard = ctx.Request().Header("Guard") | ||
} | ||
|
||
token := ctx.Request().Header("Authorization", "") | ||
if token == "" { | ||
ctx.Request().AbortWithStatus(http.StatusUnauthorized) | ||
return | ||
} | ||
|
||
if _, err := facades.Auth(ctx).Guard(guard).Parse(token); err != nil { | ||
if errors.Is(err, auth.ErrorTokenExpired) { | ||
// Refresh token | ||
token, err = facades.Auth(ctx).Guard(guard).Refresh() | ||
if err != nil { | ||
// Refresh time exceeded | ||
ctx.Request().AbortWithStatus(http.StatusUnauthorized) | ||
return | ||
} | ||
|
||
token = "Bearer " + token | ||
} else { | ||
// Token is invalid | ||
ctx.Request().AbortWithStatus(http.StatusUnauthorized) | ||
return | ||
} | ||
} | ||
|
||
// You can get User in DB and set it to ctx | ||
|
||
//var user models.User | ||
//if err := facades.Auth().User(ctx, &user); err != nil { | ||
// ctx.Request().AbortWithStatus(http.StatusUnauthorized) | ||
// return | ||
//} | ||
//ctx.WithValue("user", user) | ||
|
||
ctx.Response().Header("Authorization", token) | ||
ctx.Request().Next() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,9 @@ func init() { | |
"user": map[string]any{ | ||
"driver": "jwt", | ||
}, | ||
"admin": map[string]any{ | ||
"driver": "jwt", | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package routes | ||
|
||
import ( | ||
"github.com/goravel/framework/facades" | ||
|
||
"goravel/app/http/controllers" | ||
"goravel/app/http/middleware" | ||
) | ||
|
||
func Api() { | ||
authController := controllers.NewAuthController() | ||
facades.Route().Post("auth/login", authController.Login) | ||
facades.Route().Middleware(middleware.Auth()).Get("auth/info", authController.Info) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters