This is a basic demo of how to build a basic 8Base powered application using Nuxt as a frontend.
Too many demo app's skip over the REAL things you will need to do. This demo hopefully is a complete example of a basic working app.
It does the following:
- Setup 8Base backend
- Code for sign up, sign in and logout
- Create data on the backend
- Upload an image
- Persist store data between reloads
- Write custom functions on the backend server
Please see the corresponding backend codebase here.
- Will need more than the Free Tier of 8Base to use good Auth.
Go to: https://app.8base.com/data
-
New table > Posts
-
Add fields (caption, image)
-
New table > Likes
- Drag Posts onto empty field
- Rename field to Posts(not Posts)
Go to: https://app.8base.com/data, use the "Data" tab to manually make some Posts and Likes.
- Add a Post using 8Base dashboard
- Add a Like using 8Base dashboard, connect to Post
SEE https://app.8base.com/api-explorer
- Go here: https://app.8base.com/app-services/authentication
- SEE https://www.youtube.com/watch?v=CHIElGa4Hug&feature=youtu.be
- For the
Auth0 Database Name
field, the free version of Auth0 doesn't have that feature. You can just enterUsername-Password-Authentication
though, and it will work. - In Auth0 Dashboard go to
Applications > Your App > Show Advanced Settings > Grant Types
and make surepassword
is ticked.
Note the Auth Profile ID
, you will need it for the GQL Query.
Here we install Nuxt and start writing code
SEE https://nuxtjs.org/guide/installation#starting-from-scratch
npm install --save nuxt
Then:
- Make
package.json
- Make
nuxt.config.js
- Make
store/index.vue
npm install --save-dev @nuxtjs/dotenv
Then:
- Add
@nuxtjs/dotenv
to thebuildModules
section ofnuxt.config.js
- Duplicate
example.env
and setup your.env
file. - Get
BASE_ENDPOINT
URL from: https://app.8base.com/settings/general - Get
AUTH_PROFILE_ID
from https://app.8base.com/app-services/authentication
SEE https://github.com/nuxt-community/apollo-module#setup
npm install --save @nuxtjs/apollo
Then:
- Add
@nuxtjs/apollo
tomodules
ofnuxt.config.js
- Create
/plugins/apollo-config-default.js
SEE https://www.npmjs.com/package/filestack-js SEE https://filestack.github.io/filestack-js/
npm install --save filestack-js
- Create Vue template with form at
pages/signup
- Run
npm run dev
- Go to http://localhost:3000/signup
- Create
/store/index.js
- Create action for
USER_SIGNUP
- Create
gql/mutations/UserSignup.gql
. Use theuserSignUpWithPassword
mutation.
- Create a
USER_LOGIN
- Create
gql/mutations/UserLogin.gql
. Use theuserLogin
mutation. - Set
this.$apolloHelpers.onLogin(token)
. - Save token to store, helps for the refresh token later.
- Get user and save to store, see next step.
- Redirect to protected route?
- Create a
USER_GET
action. - Create
gql/queries/User.gql
. Use theUser
query.
- Create a
middleware/authenticated.js
file - Add
middleware: ["authenticated"]
to each page route you want to be protected.
TIP Now would be a good time to persist your user data. SEE https://www.npmjs.com/package/nuxt-vuex-localstorage
NOTE If you use nuxt-vuex-localstorage
keep in mind that the server will not have access to your browsers local storage, so be on the look out for node mismatch SSR errors. Best to avoid this using nuxtServerInit
hook for required data and use of <client-only>
. Another good idea is to use route params for data required to make SSR requests.
- Create a form on
/pages/home.vue
- Add
<button-upload>
component, captureuploadedAll
event. - Use
postCreate
GQL mutation on form submit. Use emitted FilestackfileId
from<button-upload>
component.- Note the
create
relationship syntax in GQL. There is alsoconnect
andreconnect
.create
creates the image while also creating the post.connect
would connect to an image that already existed in 8Base somewhere.reconnect
would be used if you could connect multiple images to a post, and you wanted to RESET all connections.
- Note the
- The
refecthQueries
feature is useful for a basic alternative to a websocket subscription.
- Make a smart query, using the
postsList
GQL query.- Note the
orderBy
parameter in the query.
- Note the
- Be sure to use the
update()
function to return what you need. - Note the use of
$apollo.loading
in your templates.
- Use
this.$apolloHelpers.onLogout()
in a async store action, useful to clear store too. - Then redirect to login page.
SEE https://app.8base.com/app-services/roles
The "Custom Filters" are just GQL filter: {}
objects, so you can mock them in the API Explorer.
Some devDependencies
for better developer experience:
eslint
eslint-plugin-vue
prettier
prettier-eslint
sass-loader
Use this to make your Vuex data work between reloads! SEE https://www.npmjs.com/package/nuxt-vuex-localstorage
Use lodash _get()
a lot!
SEE https://www.npmjs.com/package/lodash