curl --request GET \
--url https://api.example.com/api/auth/verify \
--header 'Authorization: <authorization>'{
"valid": true,
"user_id": 123,
"username": "<string>"
}Verify the validity of an access token
curl --request GET \
--url https://api.example.com/api/auth/verify \
--header 'Authorization: <authorization>'{
"valid": true,
"user_id": 123,
"username": "<string>"
}GET /api/auth/verify
Bearer <access_token>curl -X GET https://vega.benidevo.com/api/auth/verify \
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
{
"valid": true,
"user_id": 123,
"username": "john_doe"
}
{
"error": "missing authorization header"
}
{
"error": "invalid authorization header format"
}
Bearer <token>
{
"error": "invalid or expired token"
}
async function makeAuthenticatedRequest(endpoint) {
// First, verify the token
const verifyResponse = await fetch('/api/auth/verify', {
headers: {
'Authorization': `Bearer ${accessToken}`
}
});
if (!verifyResponse.ok) {
// Token is invalid, refresh it
const newToken = await refreshAccessToken();
accessToken = newToken;
}
// Now make the actual request
return fetch(endpoint, {
headers: {
'Authorization': `Bearer ${accessToken}`
}
});
}
async function getCurrentUser() {
const response = await fetch('/api/auth/verify', {
headers: {
'Authorization': `Bearer ${accessToken}`
}
});
if (response.ok) {
const data = await response.json();
return {
id: data.user_id,
username: data.username
};
}
throw new Error('Not authenticated');
}
// Check if user is authenticated
async function isAuthenticated() {
try {
const response = await fetch('/api/auth/verify', {
headers: {
'Authorization': `Bearer ${accessToken}`
}
});
return response.ok && (await response.json()).valid;
} catch (error) {
return false;
}
}
internal/api/auth/handlers.go:VerifyToken.
func (h *AuthAPIHandler) VerifyToken(ctx *gin.Context) {
authHeader := ctx.GetHeader("Authorization")
if authHeader == "" {
ctx.JSON(http.StatusUnauthorized, gin.H{"error": "missing authorization header"})
return
}
const bearerPrefix = "Bearer "
if len(authHeader) < len(bearerPrefix) || authHeader[:len(bearerPrefix)] != bearerPrefix {
ctx.JSON(http.StatusUnauthorized, gin.H{"error": "invalid authorization header format"})
return
}
token := authHeader[len(bearerPrefix):]
claims, err := h.authService.VerifyToken(token)
if err != nil {
ctx.JSON(http.StatusUnauthorized, gin.H{"error": "invalid or expired token"})
return
}
ctx.JSON(http.StatusOK, gin.H{
"valid": true,
"user_id": claims.UserID,
"username": claims.Username,
})
}
TOKEN_SECRET configured in your environment variables.