-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add authentication provider tokenAuthProvider
* feat: add authentication provider tokenAuthProvider * doc: update tokenAuthProvider docs
- Loading branch information
Showing
10 changed files
with
282 additions
and
11 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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { AuthProvider, fetchUtils } from 'ra-core'; | ||
|
||
export interface Options { | ||
obtainAuthTokenUrl?: string; | ||
} | ||
|
||
function tokenAuthProvider(options: Options = {}): AuthProvider { | ||
const opts = { | ||
obtainAuthTokenUrl: '/api-token-auth/', | ||
...options, | ||
}; | ||
return { | ||
login: async ({ username, password }) => { | ||
const request = new Request(opts.obtainAuthTokenUrl, { | ||
method: 'POST', | ||
body: JSON.stringify({ username, password }), | ||
headers: new Headers({ 'Content-Type': 'application/json' }), | ||
}); | ||
const response = await fetch(request); | ||
if (response.ok) { | ||
localStorage.setItem('token', (await response.json()).token); | ||
return; | ||
} | ||
if (response.headers.get('content-type') !== 'application/json') { | ||
throw new Error(response.statusText); | ||
} | ||
|
||
const json = await response.json(); | ||
const error = json.non_field_errors; | ||
throw new Error(error || response.statusText); | ||
}, | ||
logout: () => { | ||
localStorage.removeItem('token'); | ||
return Promise.resolve(); | ||
}, | ||
checkAuth: () => | ||
localStorage.getItem('token') ? Promise.resolve() : Promise.reject(), | ||
checkError: error => { | ||
const status = error.status; | ||
if (status === 401 || status === 403) { | ||
localStorage.removeItem('token'); | ||
return Promise.reject(); | ||
} | ||
return Promise.resolve(); | ||
}, | ||
getPermissions: () => { | ||
return Promise.resolve(); | ||
}, | ||
}; | ||
} | ||
|
||
export function createOptionsFromToken() { | ||
const token = localStorage.getItem('token'); | ||
if (!token) { | ||
return {}; | ||
} | ||
return { | ||
user: { | ||
authenticated: true, | ||
token: 'Token ' + token, | ||
}, | ||
}; | ||
} | ||
|
||
export function fetchJsonWithAuthToken(url: string) { | ||
return fetchUtils.fetchJson(url, createOptionsFromToken()); | ||
} | ||
|
||
export default tokenAuthProvider; |
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,98 @@ | ||
import 'cross-fetch/polyfill'; | ||
import fetchMock from 'fetch-mock-jest'; | ||
|
||
import tokenAuthProvider, { | ||
createOptionsFromToken, | ||
} from '../src/tokenAuthProvider'; | ||
|
||
fetchMock.config.overwriteRoutes = true; | ||
|
||
describe('login', () => { | ||
const LOGIN_DATA = { | ||
username: 'foo', | ||
password: 'example', | ||
}; | ||
|
||
it('should throw error with statusText for non json responses', async () => { | ||
fetchMock.post('/api-token-auth/', () => { | ||
return 404; | ||
}); | ||
await expect(tokenAuthProvider().login(LOGIN_DATA)).rejects.toThrow( | ||
'Not Found' | ||
); | ||
}); | ||
|
||
it('should throw error with non_field_errors', async () => { | ||
const error = 'Unable to log in with provided credentials.'; | ||
fetchMock.post('/api-token-auth/', { | ||
body: { | ||
non_field_errors: [error], | ||
}, | ||
status: 400, | ||
}); | ||
await expect(tokenAuthProvider().login(LOGIN_DATA)).rejects.toThrow(error); | ||
}); | ||
|
||
it('should set token when successfull', async () => { | ||
const token = 'abcdef'; | ||
fetchMock.post('/api-token-auth/', { | ||
body: { token }, | ||
}); | ||
await tokenAuthProvider().login(LOGIN_DATA); | ||
expect(localStorage.getItem('token')).toBe(token); | ||
}); | ||
}); | ||
|
||
describe('logout', () => { | ||
it('should remove token', async () => { | ||
localStorage.setItem('token', 'abcdef'); | ||
await tokenAuthProvider().logout({}); | ||
}); | ||
}); | ||
|
||
describe('checkAuth', () => { | ||
it('should return resolve when token exists', async () => { | ||
localStorage.setItem('token', 'abcdef'); | ||
await expect(tokenAuthProvider().checkAuth({})).resolves.toBeUndefined(); | ||
}); | ||
it('should return reject when token does not exists', async () => { | ||
localStorage.clear(); | ||
await expect(tokenAuthProvider().checkAuth({})).rejects.toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe('checkError', () => { | ||
it('should remove token and reject for 401 or 403 error', async () => { | ||
[401, 403].forEach(async status => { | ||
await expect( | ||
tokenAuthProvider().checkError({ status }) | ||
).rejects.toBeUndefined(); | ||
}); | ||
}); | ||
it('should resolve on other errors', async () => { | ||
await expect( | ||
tokenAuthProvider().checkError({ status: 500 }) | ||
).resolves.toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe('getPermissions', () => { | ||
it.todo('missing implementation'); | ||
}); | ||
|
||
describe('createOptionsFromToken', () => { | ||
test('with token', () => { | ||
localStorage.setItem('token', 'abcdef'); | ||
expect(createOptionsFromToken()).toEqual({ | ||
user: { | ||
authenticated: true, | ||
token: 'Token abcdef', | ||
}, | ||
}); | ||
}); | ||
|
||
test('without token', () => { | ||
localStorage.clear(); | ||
expect(createOptionsFromToken()).toEqual({}); | ||
}); | ||
}); |
Oops, something went wrong.