Coder Social home page Coder Social logo

gah's Introduction

Gin Auth Handlers

Installation

To install Gah (Gin Auth Handlers) package, you need to install Go and set your Go workspace first.

Related posts

dev.to Authentication with gin-gonic && gah for golang

You can use the below Go command to install Gah.

go get -u github.com/yasaricli/gah

Usage and documentation

First let's set the required environment variables

export MONGO_URL=mongodb://127.0.0.1:27017 # MongoDB server URL.
export MONGO_DATABASE=project_db # MongoDB Project db name
export MONGO_COLLECTION=users # Collection to register all users

Use with gin-gonic

You need the gin package to use gah. You can install it as follows.

go get -u github.com/gin-gonic/gin

Add the LoginHandler and RegisterHandler functions to the API.

package main

import (
  "github.com/gin-gonic/gin"
  "github.com/yasaricli/gah"
)

func main() {
  router := gin.Default()
   
  api := router.Group("/api")
  {
    api.POST("/login", gah.LoginHandler)
    api.POST("/register", gah.RegisterHandler)
  }

  router.Run(":4000")
}

AuthRequiredMiddleware

Let's make a handler where the user is required:

Default: Get user ID and auth token from X-User-Id and X-Auth-Token headers

func main() {
  router := gin.Default()
   
  api := router.Group("/api")
  {
    api.GET("/books", gah.AuthRequiredMiddleware, func(c *gin.Context) {
      userID := c.MustGet("userID")
      
      c.JSON(200, gin.H{
        "userId": userID,
      })
    })
  }

  router.Run(":4000")
}

Production Release Mode

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:	export GIN_MODE=release
 - using code:	gin.SetMode(gin.ReleaseMode)

gah's People

Contributors

yasaricli avatar

Stargazers

Gábor Mihálcz avatar

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.