Skip to content

Commit 9b09459

Browse files
jmschneiderpi0
authored andcommitted
feat: laravel passport provider (#157)
1 parent 7202cc0 commit 9b09459

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed

docs/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* [Facebook](providers/facebook.md)
1515
* [Github](providers/github.md)
1616
* [Google](providers/google.md)
17+
* [Laravel Passport](providers/passport.md)
1718
* API
1819
* [Auth](api/auth.md)
1920
* [Storage](api/storage.md)

docs/providers/passport.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Laravel Passport
2+
3+
[Source Code](https://github.com/nuxt-community/auth-module/blob/dev/lib/providers/passport.js)
4+
5+
## Usage
6+
7+
```js
8+
auth: {
9+
strategies: {
10+
'laravel.passport': {
11+
url: '...',
12+
client_id: '...',
13+
client_secret: '...'
14+
},
15+
}
16+
}
17+
```
18+
19+
## Usage
20+
21+
Anywhere in your application logic:
22+
23+
```js
24+
this.$auth.loginWith('passport')
25+
```
26+
27+
💁 This provider is based on [oauth2 scheme](../schemes/oauth2.md) and supports all scheme options.
28+
29+
### Obtaining `url`, `client_id` and `client_secret`
30+
31+
These options are **REQUIRED**. The `url` is the location of your Laravel application. To obtain the `client_id` and `client_secret`, create a new client app in your [Laravel app](https://laravel.com/docs/5.6/passport#managing-clients).
32+

lib/providers/laravel.passport.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
const axios = require('axios')
2+
const bodyParser = require('body-parser')
3+
const { assignDefaults } = require('./_utils')
4+
5+
module.exports = function laravelPassport (strategy) {
6+
assignDefaults(strategy, {
7+
_scheme: 'oauth2',
8+
_name: 'laravel.passport',
9+
authorization_endpoint: `${strategy.url}/oauth/authorize`,
10+
token_endpoint: `${strategy.url}/oauth/token`,
11+
token_key: 'access_token',
12+
token_type: 'Bearer',
13+
response_type: 'code',
14+
grant_type: 'authorization_code',
15+
scope: '*'
16+
})
17+
18+
// Get client_secret, client_id and token_endpoint
19+
const clientSecret = strategy.client_secret
20+
const clientID = strategy.client_id
21+
const tokenEndpoint = strategy.token_endpoint
22+
23+
// IMPORTANT: remove client_secret from generated bundle
24+
delete strategy.client_secret
25+
26+
// Endpoint
27+
const endpoint = `/_auth/oauth/${strategy._name}/authorize`
28+
strategy.access_token_endpoint = endpoint
29+
30+
// Set response_type to code
31+
strategy.response_type = 'code'
32+
33+
// Form parser
34+
const formMiddleware = bodyParser.urlencoded()
35+
36+
// Register endpoint
37+
this.options.serverMiddleware.unshift({
38+
path: endpoint,
39+
handler: (req, res, next) => {
40+
if (req.method !== 'POST') {
41+
return next()
42+
}
43+
44+
formMiddleware(req, res, () => {
45+
const {
46+
code,
47+
redirect_uri: redirectUri = strategy.redirect_uri,
48+
response_type: responseType = strategy.response_type,
49+
grant_type: grantType = strategy.grant_type
50+
} = req.body
51+
52+
if (!code) {
53+
return next()
54+
}
55+
56+
axios
57+
.request({
58+
method: 'post',
59+
url: tokenEndpoint,
60+
data: {
61+
client_id: clientID,
62+
client_secret: clientSecret,
63+
grant_type: grantType,
64+
response_type: responseType,
65+
redirect_uri: redirectUri,
66+
code
67+
},
68+
headers: {
69+
Accept: 'application/json'
70+
}
71+
})
72+
.then(response => {
73+
res.end(JSON.stringify(response.data))
74+
})
75+
.catch(error => next(error))
76+
})
77+
}
78+
})
79+
}

lib/schemes/oauth2.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ export default class Oauth2Scheme {
9999
const data = await this.$auth.request({
100100
method: 'post',
101101
url: this.options.access_token_endpoint,
102+
baseURL: false,
102103
data: encodeQuery({
103104
code: parsedQuery.code,
104105
client_id: this.options.client_id,

0 commit comments

Comments
 (0)