Logout the current user
curl --request POST \
--url http://localhost:2000/auth/logout \
--header 'Authorization: Bearer <token>'import requests
url = "http://localhost:2000/auth/logout"
headers = {"Authorization": "Bearer <token>"}
response = requests.post(url, headers=headers)
print(response.text)const options = {method: 'POST', headers: {Authorization: 'Bearer <token>'}};
fetch('http://localhost:2000/auth/logout', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_PORT => "2000",
CURLOPT_URL => "http://localhost:2000/auth/logout",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"net/http"
"io"
)
func main() {
url := "http://localhost:2000/auth/logout"
req, _ := http.NewRequest("POST", url, nil)
req.Header.Add("Authorization", "Bearer <token>")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("http://localhost:2000/auth/logout")
.header("Authorization", "Bearer <token>")
.asString();require 'uri'
require 'net/http'
url = URI("http://localhost:2000/auth/logout")
http = Net::HTTP.new(url.host, url.port)
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
response = http.request(request)
puts response.read_body{
"message": "Successfully logged out John Doe",
"data": {}
}OAuth & Sessions
Logout
Logs out the currently authenticated user by invalidating their session token. Requires a valid authentication token. After calling this endpoint remove the user data from the client.
POST
/
auth
/
logout
Logout the current user
curl --request POST \
--url http://localhost:2000/auth/logout \
--header 'Authorization: Bearer <token>'import requests
url = "http://localhost:2000/auth/logout"
headers = {"Authorization": "Bearer <token>"}
response = requests.post(url, headers=headers)
print(response.text)const options = {method: 'POST', headers: {Authorization: 'Bearer <token>'}};
fetch('http://localhost:2000/auth/logout', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_PORT => "2000",
CURLOPT_URL => "http://localhost:2000/auth/logout",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"net/http"
"io"
)
func main() {
url := "http://localhost:2000/auth/logout"
req, _ := http.NewRequest("POST", url, nil)
req.Header.Add("Authorization", "Bearer <token>")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("http://localhost:2000/auth/logout")
.header("Authorization", "Bearer <token>")
.asString();require 'uri'
require 'net/http'
url = URI("http://localhost:2000/auth/logout")
http = Net::HTTP.new(url.host, url.port)
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
response = http.request(request)
puts response.read_body{
"message": "Successfully logged out John Doe",
"data": {}
}Logout the current user by invalidating their session token. Requires a valid authentication token. After calling this endpoint remove the user data from the client.
Request
Headers
| Name | Type | Required | Description |
|---|---|---|---|
| Authorization | string | Yes | Bearer token |
Response
200 OK - Successfully logged out
{
"message": "Successfully logged out John Doe",
"data": {}
}
401 Unauthorized
{
"error": {
"code": "UNAUTHORIZED",
"message": "Missing or invalid authentication token"
}
}
500 Internal Server Error
{
"error": {
"code": "SERVER_ERROR",
"message": "Internal server error"
}
}
Example
curl -X POST 'http://localhost:2000/auth/logout' \
-H 'Authorization: Bearer your-jwt-token'
Notes
- This endpoint requires a valid JWT token in the Authorization header
- The server will invalidate the session token, making it unusable for future requests
- After successful logout, remove all user data from client-side storage
- Clear any stored tokens, user information, or session data from your application
- The response includes a personalized message with the user’s name
- This is the recommended way to properly log out users from the system
Client-side Implementation
After calling this endpoint, you should:- Remove the JWT token from storage (localStorage, sessionStorage, cookies)
- Clear user data from your application state
- Redirect the user to the login page
- Clear any cached data that requires authentication
// Example client-side logout implementation
async function logout() {
try {
const response = await fetch('http://localhost:2000/auth/logout', {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`
}
});
// Clear client-side data
localStorage.removeItem('authToken');
localStorage.removeItem('userData');
// Redirect to login
window.location.href = '/login';
} catch (error) {
console.error('Logout failed:', error);
}
}
Was this page helpful?