๋ค์ ์ฝ๋๊ฐ ์์ต๋๋ค.
import feathers from 'feathers/client';
import rest from 'feathers-rest/client';
import request from 'browser-request';
import config from './config';
import User from 'app/utilities/user';
const client = request.defaults({
headers: {
Authorization: `Bearer ${User.get().token}`
}
});
export default feathers().configure(
rest(config.host).request(client)
);
๊ทธ๋ฌ๋ ์๋น์ค์์ create
๋ฅผ ํธ์ถํ๋ฉด Authorization
ํค๋๋ฅผ ๋ณด๋ด์ง ์์ต๋๋ค.
๋๋ ํน์ ํค๋๋ฅผ ๋ฐ๋ผ ๋ณด๋ด๋๋ก ์ํผ์์ด์ ํธ๋ฅผ ์ฌ์ฉํ์ฌ ํ๋๋ฅผ ์ค์ ํ๋ ค๋ฉด ์ด๋ป๊ฒ ํด์ผ ํฉ๋๊น?
๊ฐ์ฌ ํด์!
์ํผ์์ด์ ํธ ๋ฐ ๊ธฐํ ์ง์ REST ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๊ฒฝ์ฐ ์๋น์ค๋ฅผ ์์ฒญํ ๋ params.headers
๋ฅผ ์ค์ ํ ์๋ ์์ต๋๋ค.
app.service('todos').find({ headers: {
Authorization: `Bearer ${User.get().token}`
}
});
feathers-authentication ํด๋ผ์ด์ธํธ์์ ์ฐ๋ฆฌ๋ ๊ทธ๊ฒ์ ํํฌ์์ ์ฌ์ฉํ๊ณ ์์ต๋๋ค.
์ฌ๊ธฐ์ ์์ฒญ์ ๋ฌธ์ ๋ ํค๋๊ฐ ์ด์ ํด๋น ์ค์ ์ผ๋ก ๋ฎ์ด์ฐ์ฌ์ง๋ค๋ ๊ฒ์ ๋๋ค(๋น์ด ์๋ ๊ฒฝ์ฐ์๋).
์์ฒญ๋! :์ค๋ง์ผ: ์๋ฒฝํ๊ฒ ์๋ํ์ต๋๋ค.
์ด๊ฒ์ผ๋ก ๋๋ฌ์ต๋๋ค :
import feathers from 'feathers/client';
import rest from 'feathers-rest/client';
import hooks from 'feathers-hooks';
import superagent from 'superagent';
import User from 'app/utilities/user';
import config from './config';
const app = feathers()
.configure(hooks())
.configure(
rest(config.host).superagent(superagent)
);
function authHook(hook) {
hook.params.headers = Object.assign({}, {
Authorization: `Bearer ${User.get().token}`
}, hook.params.headers);
}
export default {
service(url) {
let s = app.service(url);
s.before({
all: authHook
});
return s;
}
};
์ข์ ๋ณด์ธ๋ค! ํ ์ ์๋ ๋ ๋ค๋ฅธ ์ผ์ ์๋น์ค ๋ฏน์ค์ธ์ผ๋ก ๋ง๋ค์ด ๋ชจ๋ ์๋น์ค์ ์๋์ผ๋ก ์ถ๊ฐ๋๋๋ก ํ๋ ๊ฒ์ ๋๋ค.
app.mixins.push(function(service) {
service.before(authHook);
});
Mixin์ ์์ง ๊ณต๊ฐ์ ์ผ๋ก ๋ฌธ์ํ๋์ง ์์์ง๋ง ๋ชจ๋ ํ๋ฌ๊ทธ์ธ์ด ์๋น์ค์ ์ฐ๊ฒฐํ๋ ๋ฐ ์ฌ์ฉํฉ๋๋ค.
์ด ๋ฌธ์ ๋ ๋ซํ ํ ์ต๊ทผ ํ๋์ด ์์๊ธฐ ๋๋ฌธ์ ์๋์ผ๋ก ์ ๊ฒผ์ต๋๋ค. ๊ด๋ จ ๋ฒ๊ทธ์ ๋ํด์๋ ์ด ๋ฌธ์ ์ ๋ํ ๋งํฌ๊ฐ ์๋ ์ ๋ฌธ์ ๋ฅผ ์ฌ์ญ์์ค.