Giter VIP home page Giter VIP logo

reddit-clone's Introduction

Reddit clone

This is a Next.js based reddit clone, using Google authentication through NextAuth. Webapp talks to NextJS API using Apollo GraphQL, and data is stored in Firestore.

Demo: reddit-clone-tomfa.vercel.app

Thoughts on tech choice

If you want a different database, you can quite easily replace Firebase completely, as it's isolated to a small part of the backend. See Configuration section below

If you instead want to keep Firestore, you can embrace Firebase to get many perks: get realtime updates, eliminate the need for a backend, lower costs and increase performance by using Firebase Auth and making storage calls from frontend by adding Firestore security rules. This would require a larger rewrite.

Setup

  • Copy .env to .env.local. We can safely store secrets here without having them commited.

  • Set JWT_SECRET to a random long string

Auth and Firebase

  1. Create a Firebase project.

  2. In Firebase, create and download new private key. You can do this from Project Setting -> Service acccounts. From this file, copy over the following attributes to your .env.local file

    • project_id -> FIREBASE_PROJECT_ID
    • private_key -> FIREBASE_PRIVATE_KEY
    • client_email -> FIREBASE_CLIENT_EMAIL
  3. Go to Google Cloud console: Credentials for your project

    Find the existing OAuth2.0 web client (autogenerated), and click the Download icon. Add them to your .env.local file

    • Client ID -> GOOGLE_CLIENT_ID
    • Client secret -> GOOGLE_CLIENT_SECRET

    Then click the Edit icon, and add:

    • Authorised JavaScript origins: http://localhost:3000

    • Authorised redirect URIs: http://localhost:3000/api/auth/callback/google

    Replace "http://localhost:3000" with your domain when deploying the app to new domains.

Note: When running the first time, the app will display errors about disabled services or missing indexes. The errors will link you to the related places in Firebase for enabling these services or creating indexes.

When seeing errors like the one above, click the link to enable database or create indexes.

Deploying to Vercel

Deploy with Vercel

  1. Click the button above to copy and deploy to Vercel.
  2. In the guide, populate environment variables from your .env.local.
  3. After having set up a Vercel app with a domain, go back to step 3 in the previous section and add new domains to Authorised JavaScript origins and redirect urls.
  4. In Vercel, set the domain of the app to NEXTAUTH_URL env var.

Development

yarn start

This will start app at localhost:3000

Making API changes

The Webapp talks to our API via a GraphQL, using Documents and React hooks autogenerated by codegen and gqlg.

To make API changes:

  • Update ./graphql/schema.graphql
  • Run yarn schema:generate

Typescript errors can then be found with yarn tsc -b, which should notify you of most code that needs an update.

Configuration

Change logo

The logo/title "raidit" can be overriden by setting env var NEXT_PUBLIC_TITLE.

Adding / removing categories

These are set in lib/config.ts. Simply update them there.

Changing database

Everything related to using Firestore as a database is isolated to the following two files.

backend/db/index.ts and backend/db/firebase.ts

If you want to replace the database, you need only reimplement these methods.

Restricting domains for login

If you want to restrict login to users with @example.com domain, specify ALLOWED_SIGNUP_DOMAINS in .env. You can use a comma , separator to specify multiple domains.

By default, all domains are granted access.

Changing authentication methods

You can add Auth0, SMS or email login, by adding NextAuth providers in pages/api/auth/[...nextauth].ts.

reddit-clone's People

Contributors

tomfa avatar

Watchers

 avatar  avatar

Forkers

wxyzed

reddit-clone's Issues

Use consistent styling

A mix of copy pasting and hacking has led to a mix of global css, inline styles, css modules and styled components.

Use... css modules!

Form validation and error handling

Everything is very optimistic and crashes easily.

  • Add minimum length for post title
  • Add minimum length for post content
  • Display an error instead of crashing if API returns error

Add filter for displaying archived posts

The use case for archiving is not really deleting posts, but marking them as completed or no longer relevant (ie. this suggestion has been implemented).

Therefore, add a "show archived" button in the Post list

Create a Postgres + Prisma version

As noted in the README

If you want a different database, you can quite easily replace Firebase completely, as it's isolated to a small part of the backend. See Configuration section below

Let's do Postgres and prisma?

Create a Firebase-embraced version

As noted in the README

If you instead want to keep Firestore, you can embrace Firebase to get many perks: get realtime updates, eliminate the need for a backend, lower costs and increase performance by using Firebase Auth and making storage calls from frontend by adding Firestore security rules. This would require a larger rewrite.

Let's do this larger rewrite, together with reactfire ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.