Skip to content

Instantly share code, notes, and snippets.

@mtvbrianking
Last active November 1, 2022 15:20
Show Gist options
  • Save mtvbrianking/e0798dd7068ccf4c16ae77db28916933 to your computer and use it in GitHub Desktop.
Save mtvbrianking/e0798dd7068ccf4c16ae77db28916933 to your computer and use it in GitHub Desktop.
Postman API Auth Token

pre-request script

if(accessToken = pm.environment.get('access-token')) {
    console.log(`Using access token: '${accessToken}'`);
    return;
}

let req = {
    url: 'http://localhost:8000/api/auth',
    method: 'POST',
    header: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    },
    body: {
        mode: 'xml',
        raw: JSON.stringify({
            identifier: pm.environment.get('email'),
            password: pm.environment.get('password')
        })
    }
};

pm.sendRequest(req, function (error, response) {
    if (error) {
        return;
    }

    if(response.code  / 100 != 2) {
        console.warn(response.json());
        throw new Error(`[${response.code}] Failed to obtain an access token.`);
    }

    // console.log({'response': response});
    pm.environment.set('access-token', response.json().plainTextToken);
});

Test

var accessToken = pm.environment.get('access-token');

if(pm.response.code === 401) {
    console.error(`Invalid access token '${accessToken}' purged.`);

    pm.environment.set('access-token', '');
}
{
"info": {
"_postman_id": "d41bf66a-7906-4d64-ba9b-58eb020f4077",
"name": "wpapers",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "auth.store",
"request": {
"method": "POST",
"header": [
{
"key": "Accept",
"value": "application/json",
"type": "text"
}
],
"body": {
"mode": "formdata",
"formdata": [
{
"key": "identifier",
"value": "{{email}}",
"type": "text"
},
{
"key": "password",
"value": "{{password}}",
"type": "text"
}
],
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://localhost:8000/api/auth",
"protocol": "http",
"host": [
"localhost"
],
"port": "8000",
"path": [
"api",
"auth"
]
}
},
"response": []
},
{
"name": "auth.destory",
"event": [
{
"listen": "test",
"script": {
"exec": [
"var accessToken = pm.environment.get('access-token');",
"",
"if(pm.response.code / 100 != 2) {",
" console.log(`Unset current access token '${accessToken}'.`);",
"",
" pm.environment.set('access-token', '');",
"}"
],
"type": "text/javascript"
}
}
],
"request": {
"auth": {
"type": "bearer",
"bearer": [
{
"key": "token",
"value": "{{access-token}}",
"type": "string"
}
]
},
"method": "DELETE",
"header": [
{
"key": "Accept",
"value": "application/json",
"type": "text"
}
],
"url": {
"raw": "http://localhost:8000/api/auth",
"protocol": "http",
"host": [
"localhost"
],
"port": "8000",
"path": [
"api",
"auth"
]
}
},
"response": []
},
{
"name": "users.index",
"event": [
{
"listen": "prerequest",
"script": {
"exec": [
""
],
"type": "text/javascript"
}
},
{
"listen": "test",
"script": {
"exec": [
""
],
"type": "text/javascript"
}
}
],
"request": {
"auth": {
"type": "bearer",
"bearer": [
{
"key": "token",
"value": "{{access-token}}",
"type": "string"
}
]
},
"method": "GET",
"header": [
{
"key": "Accept",
"value": "application/json",
"type": "text"
},
{
"key": "",
"value": "",
"type": "text",
"disabled": true
}
],
"url": {
"raw": "http://localhost:8000/api/users",
"protocol": "http",
"host": [
"localhost"
],
"port": "8000",
"path": [
"api",
"users"
]
}
},
"response": []
}
],
"event": [
{
"listen": "prerequest",
"script": {
"type": "text/javascript",
"exec": [
"if(accessToken = pm.environment.get('access-token')) {",
" console.log(`Using access token: '${accessToken}'`);",
" return;",
"}",
"",
"let req = {",
" url: 'http://localhost:8000/api/auth',",
" method: 'POST',",
" header: {",
" 'Accept': 'application/json',",
" 'Content-Type': 'application/json'",
" },",
" body: {",
" mode: 'xml',",
" raw: JSON.stringify({",
" identifier: pm.environment.get('email'),",
" password: pm.environment.get('password')",
" })",
" }",
"};",
"",
"pm.sendRequest(req, function (error, response) {",
" if (error) {",
" return;",
" }",
"",
" if(response.code / 100 != 2) {",
" console.warn(response.json());",
" throw new Error(`[${response.code}] Failed to obtain an access token.`);",
" }",
"",
" // console.log({'response': response});",
" pm.environment.set('access-token', response.json().plainTextToken);",
"});",
""
]
}
},
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
"var accessToken = pm.environment.get('access-token');",
"",
"if(pm.response.code === 401) {",
" console.error(`Invalid access token '${accessToken}' purged.`);",
"",
" pm.environment.set('access-token', '');",
"}"
]
}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment