This repository has been archived by the owner on Mar 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an auth API that can be used on modal dialogs (#6)
* Add an auth API that can be used on modal dialogs * Working server. Still needs integration
- Loading branch information
1 parent
eada49b
commit 8e014fe
Showing
34 changed files
with
4,197 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
Context: | ||
For 3rd-party API's, we us /src/boot/axios.js | ||
For our own API's, we use FeathersClient (socket.io & REST) | ||
https://docs.feathersjs.com/guides/basics/clients.html | ||
https://docs.feathersjs.com/api/authentication/client.html#appconfigureauthoptions | ||
Our FeathersClient is in `/src/lib/feathersClient.js` | ||
and imported into `/src/store/index.js` | ||
which is imported by Quasar's build system. /src/quasar.conf.js setting(?) | ||
Feathers-vuex integrates Vuex with FeathersClient: | ||
https://feathers-vuex.feathers-plus.com/auth-module.html | ||
Feathers-Vuex proxies it's authentication/logout actions to FeathersClient | ||
https://github.com/feathers-plus/feathers-vuex/blob/master/src/auth-module/actions.js | ||
The parameters for these actions are here: | ||
https://docs.feathersjs.com/api/authentication/client.html#appauthenticateoptions | ||
In addition to this module, you can use FeathersVuex state in UI from here: | ||
https://feathers-vuex.feathers-plus.com/auth-module.html | ||
This module: | ||
Create a Feathers Auth integration for Vue as a Quasar Boot Module. | ||
// Use case: test if user is authenticated | ||
if (Vue.$auth.currentUser()) { ... } | ||
// Use case: get current user's email | ||
name = Vue.$auth.currentUser("email") || "anonymous" | ||
// Use case: Login | ||
Vue.$auth.login({ | ||
strategy: 'local', | ||
email: '[email protected]', | ||
password: 'my-password' | ||
}); | ||
// Use case: Logout | ||
// logs out and sends message | ||
let p = Vue.$auth.logout(); | ||
// After logout, go home | ||
p.then(() => { | ||
// User data still in browser | ||
router.push({ name: "home"}); | ||
// To clear user data, do a hard refresh/redirect - https://feathers-vuex.feathers-plus.com/common-patterns.html#clearing-data-upon-user-logout | ||
location && location.reload(true) | ||
}); | ||
*/ | ||
|
||
export default ({ app, router, store, Vue }) => { | ||
// Create the API demonstrated above | ||
const auth = { | ||
currentUser(prop) { | ||
let u = store.state.auth.user || false; | ||
if (u && prop) return u[prop]; | ||
return u; | ||
}, | ||
login(authData, quiet) { | ||
return store | ||
.dispatch("auth/authenticate", authData) | ||
.then(() => { | ||
Vue.prototype.$q.notify({ | ||
message: "Right on, let's do this!", | ||
type: "info" | ||
}); | ||
}) | ||
.catch(err => { | ||
if (!quiet) { | ||
console.log(err); | ||
Vue.prototype.$q.notify({ | ||
message: "There was a problem logging you in.", | ||
type: "error" | ||
}); | ||
} | ||
}); | ||
}, | ||
logout(quiet) { | ||
return store.dispatch("auth/logout").then(() => { | ||
if (!quiet) | ||
Vue.prototype.$q.notify({ | ||
message: "You've been logged out.", | ||
type: "info" | ||
}); | ||
}); | ||
}, | ||
register(authData) { | ||
// FIXME why is this empty? | ||
} | ||
}; | ||
|
||
// Auth from JWT stored in browser before loading the app. true => suppress token not found error | ||
auth.login("jwt", true); | ||
|
||
// Add API to Vue | ||
Vue.prototype.$auth = auth; | ||
|
||
// If you would like to play with it in the console, uncomment this line: | ||
// console.log(auth); | ||
|
||
// Then, in the console: | ||
/* | ||
temp1.login({ | ||
strategy: "local", | ||
email: "[email protected]", | ||
password: "secret" | ||
}) | ||
*/ | ||
|
||
// If you haven't created this user, see here: | ||
// https://docs.feathersjs.com/guides/chat/authentication.html | ||
// For this REST api endpoint | ||
/* | ||
curl 'http://localhost:3001/users/' -H 'Content-Type: application/json' --data-binary '{ "email": "[email protected]", "password": "secret" }' | ||
*/ | ||
}; |
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,34 @@ | ||
import feathers from "@feathersjs/feathers"; | ||
import auth from "@feathersjs/authentication-client"; | ||
import { CookieStorage } from "cookie-storage"; | ||
import restClient from "@feathersjs/rest-client"; | ||
import axios from "axios"; | ||
|
||
import socketio from "@feathersjs/socketio-client"; | ||
import io from "socket.io-client"; | ||
let socketOptions = { transports: ["websocket"] }; | ||
|
||
// Production Config | ||
|
||
let url = process.env.HOST_URL; | ||
|
||
// In dev mode, use http://localhost:3000 as the URL. | ||
// Tried to set this up with https using nginx.conf. | ||
// Dev config Quasar HMR wants to use port from quasar.conf.js, even through it's served through a proxy on 443. This is the compromise. | ||
|
||
if (["localhost:3000"].includes(window.location.host)) { | ||
// See quasar.conf.js | ||
url = "http://localhost:3001"; | ||
socketOptions.rejectUnauthorized = false; | ||
} | ||
|
||
const socket = io(url, socketOptions); | ||
const rest = restClient(url); | ||
|
||
const storage = new CookieStorage(); | ||
const feathersClient = feathers() | ||
.configure(socketio(socket)) | ||
.configure(rest.axios(axios)) // FIXME needed? | ||
.configure(auth({ storage })); // See ... for options | ||
|
||
export default feathersClient; |
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,48 @@ | ||
// This file is auto-magically imported into Quasar build system. | ||
|
||
import Vue from "vue"; | ||
import Vuex from "vuex"; | ||
import feathersVuex from "feathers-vuex"; | ||
import feathersClient from "../lib/feathersClient"; | ||
|
||
const { service, auth, FeathersVuex } = feathersVuex(feathersClient, { | ||
idField: "_id" | ||
nameStyle: 'short', // Determines the source of the module name. 'short' or 'path' | ||
enableEvents: true // Set to false to explicitly disable socket event handlers. | ||
}); | ||
|
||
Vue.use(Vuex); | ||
Vue.use(FeathersVuex); | ||
|
||
export default function(/*{ ssrContext }*/) { | ||
return new Vuex.Store({ | ||
// enable strict mode (adds overhead!) - dev mode only | ||
strict: process.env.DEV, | ||
// The state accessable in pages and components | ||
state: { | ||
myStuff: false | ||
}, | ||
mutations: { | ||
MUTATE_STUFF(state, newStuff) { | ||
state.myStuff = newStuff; | ||
}, | ||
}, | ||
actions: { | ||
getNewStuff: async function(context) { | ||
// FIXME demonstrate using FeathersVuex here. | ||
return Promise.resolve({ | ||
return 'hello world' | ||
}) | ||
.then(stuff => { | ||
context.commit("MUTATE_STUFF", stuff); | ||
}); | ||
}, | ||
}, | ||
plugins: [ | ||
service("api/stuff"), | ||
service("api/users"), | ||
// Setup the auth plugin. | ||
auth({ userService: "users" }) // the api/ is implied by nameStyle: 'short', above. FIXME Verify | ||
] | ||
}); | ||
} |
13 changes: 13 additions & 0 deletions
13
src/templates/quasarFeathersServer/server-feathers/.editorconfig
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,13 @@ | ||
# http://editorconfig.org | ||
root = true | ||
|
||
[*] | ||
indent_style = space | ||
indent_size = 2 | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false |
29 changes: 29 additions & 0 deletions
29
src/templates/quasarFeathersServer/server-feathers/.eslintrc.json
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,29 @@ | ||
{ | ||
"env": { | ||
"es6": true, | ||
"node": true, | ||
"mocha": true | ||
}, | ||
"parserOptions": { | ||
"ecmaVersion": 2017 | ||
}, | ||
"extends": "eslint:recommended", | ||
"rules": { | ||
"indent": [ | ||
"error", | ||
2 | ||
], | ||
"linebreak-style": [ | ||
"error", | ||
"unix" | ||
], | ||
"quotes": [ | ||
"error", | ||
"single" | ||
], | ||
"semi": [ | ||
"error", | ||
"always" | ||
] | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
src/templates/quasarFeathersServer/server-feathers/.gitignore
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,112 @@ | ||
# Logs | ||
logs | ||
*.log | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# Compiled binary addons (http://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directory | ||
# Commenting this out is preferred by some people, see | ||
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- | ||
node_modules | ||
|
||
# Users Environment Variables | ||
.lock-wscript | ||
|
||
# IDEs and editors (shamelessly copied from @angular/cli's .gitignore) | ||
/.idea | ||
.project | ||
.classpath | ||
.c9/ | ||
*.launch | ||
.settings/ | ||
*.sublime-workspace | ||
|
||
# IDE - VSCode | ||
.vscode/* | ||
!.vscode/settings.json | ||
!.vscode/tasks.json | ||
!.vscode/launch.json | ||
!.vscode/extensions.json | ||
|
||
### Linux ### | ||
*~ | ||
|
||
# temporary files which can be created if a process still has a handle open of a deleted file | ||
.fuse_hidden* | ||
|
||
# KDE directory preferences | ||
.directory | ||
|
||
# Linux trash folder which might appear on any partition or disk | ||
.Trash-* | ||
|
||
# .nfs files are created when an open file is removed but is still being accessed | ||
.nfs* | ||
|
||
### OSX ### | ||
*.DS_Store | ||
.AppleDouble | ||
.LSOverride | ||
|
||
# Icon must end with two \r | ||
Icon | ||
|
||
|
||
# Thumbnails | ||
._* | ||
|
||
# Files that might appear in the root of a volume | ||
.DocumentRevisions-V100 | ||
.fseventsd | ||
.Spotlight-V100 | ||
.TemporaryItems | ||
.Trashes | ||
.VolumeIcon.icns | ||
.com.apple.timemachine.donotpresent | ||
|
||
# Directories potentially created on remote AFP share | ||
.AppleDB | ||
.AppleDesktop | ||
Network Trash Folder | ||
Temporary Items | ||
.apdisk | ||
|
||
### Windows ### | ||
# Windows thumbnail cache files | ||
Thumbs.db | ||
ehthumbs.db | ||
ehthumbs_vista.db | ||
|
||
# Folder config file | ||
Desktop.ini | ||
|
||
# Recycle Bin used on file shares | ||
$RECYCLE.BIN/ | ||
|
||
# Windows Installer files | ||
*.cab | ||
*.msi | ||
*.msm | ||
*.msp | ||
|
||
# Windows shortcuts | ||
*.lnk | ||
|
||
# Others | ||
lib/ | ||
data/ |
22 changes: 22 additions & 0 deletions
22
src/templates/quasarFeathersServer/server-feathers/LICENSE
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,22 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015 Feathers | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
Oops, something went wrong.