"use strict";(self.webpackChunkgoffee_site=self.webpackChunkgoffee_site||[]).push([[8631],{3895:(e,n,t)=>{t.r(n),t.d(n,{assets:()=>a,contentTitle:()=>s,default:()=>h,frontMatter:()=>c,metadata:()=>i,toc:()=>d});var o=t(4848),r=t(8453);const c={title:"JWT Tokens"},s=void 0,i={id:"jwt",title:"JWT Tokens",description:"JWT tokens are used for authentication, Goffee provides you with ways to generate JWT tokens, decode them, and check their expiry.",source:"@site/docs/jwt.md",sourceDirName:".",slug:"/jwt",permalink:"/goffee/docs/jwt",draft:!1,unlisted:!1,tags:[],version:"current",frontMatter:{title:"JWT Tokens"},sidebar:"docsSidebar",previous:{title:"Hooks",permalink:"/goffee/docs/hooks"},next:{title:"Authentication",permalink:"/goffee/docs/authentication"}},a={},d=[{value:"JWT secret",id:"jwt-secret",level:4},{value:"Generate JWT tokens",id:"generate-jwt-tokens",level:4},{value:"Decode a token",id:"decode-a-token",level:3},{value:"Check the token expiry",id:"check-the-token-expiry",level:3}];function l(e){const n={code:"code",h3:"h3",h4:"h4",p:"p",pre:"pre",...(0,r.R)(),...e.components};return(0,o.jsxs)(o.Fragment,{children:[(0,o.jsxs)(n.p,{children:["JWT tokens are used for authentication, Goffee provides you with ways to generate ",(0,o.jsx)(n.code,{children:"JWT tokens"}),", decode them, and check their expiry.\nThe configuration for ",(0,o.jsx)(n.code,{children:"JWT tokens"})," can be found in the ",(0,o.jsx)(n.code,{children:".env"})," file and they will be injected into the environment, you can also inject these values with an external tool and that will would work too,\nThe configuration helps you define things like the ",(0,o.jsx)(n.code,{children:"secret"})," key and the ",(0,o.jsx)(n.code,{children:"validity"})," of the tokens"]}),"\n",(0,o.jsx)(n.h4,{id:"jwt-secret",children:"JWT secret"}),"\n",(0,o.jsxs)(n.p,{children:["The ",(0,o.jsx)(n.code,{children:"jwt secret"})," is a random string. it's used to sign the ",(0,o.jsx)(n.code,{children:"jwt token"}),", its recommended to have it's a minimum of ",(0,o.jsx)(n.code,{children:"6"})," characters.\nRemember to always keep your ",(0,o.jsx)(n.code,{children:"JWT secret"})," secret."]}),"\n",(0,o.jsx)(n.h4,{id:"generate-jwt-tokens",children:"Generate JWT tokens"}),"\n",(0,o.jsx)(n.p,{children:"Here is how to generate a JWT token"}),"\n",(0,o.jsx)(n.pre,{children:(0,o.jsx)(n.code,{className:"language-go",children:'package controllers\n\nimport (\n    "git.smarteching.com/goffee/core"\n)\n\nfunc Login(c *core.Context) *core.Response {\n    // prepare the payload\n    payload := map[string]interface{}{\n        "userID": 12345\n    }\n    // generate the token\n    tokenStr, err := c.GetJWT().GenerateToken(payload)\n}\n'})}),"\n",(0,o.jsx)(n.h3,{id:"decode-a-token",children:"Decode a token"}),"\n",(0,o.jsx)(n.p,{children:"To decode a token and extract the encoded information, check the code below"}),"\n",(0,o.jsx)(n.pre,{children:(0,o.jsx)(n.code,{className:"language-go",children:'package controllers\n\nimport (\n    "git.smarteching.com/goffee/core"\n)\n\nfunc Login(c *core.Context) *core.Response {\n    myToken := "my-jwt-token-string"\n    payload, err := c.GetJWT().DecodeToken(myToken)\n    if err != nil {\n        return c.Response.SetStatusCode(400).Text(err.Error())\n    }\n}\n'})}),"\n",(0,o.jsx)(n.h3,{id:"check-the-token-expiry",children:"Check the token expiry"}),"\n",(0,o.jsx)(n.p,{children:"To check if a token has expired or not, refer to the code below:"}),"\n",(0,o.jsx)(n.pre,{children:(0,o.jsx)(n.code,{className:"language-go",children:'package controllers\n\nimport (\n    "git.smarteching.com/goffee/core"\n)\n\nfunc Login(c *core.Context) *core.Response {\n    myToken := "my-jwt-token-string"\n    tokenHasExpired, err := c.GetJWT().HasExpired(myToken)\n    if err != nil {\n        return c.Response.SetStatusCode(400).Text(err.Error())\n    }\n\n    if tokenHasExpired {\n        // here token has expired\n    }\n}\n'})})]})}function h(e={}){const{wrapper:n}={...(0,r.R)(),...e.components};return n?(0,o.jsx)(n,{...e,children:(0,o.jsx)(l,{...e})}):l(e)}},8453:(e,n,t)=>{t.d(n,{R:()=>s,x:()=>i});var o=t(6540);const r={},c=o.createContext(r);function s(e){const n=o.useContext(c);return o.useMemo((function(){return"function"==typeof e?e(n):{...n,...e}}),[n,e])}function i(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(r):e.components||r:s(e.components),o.createElement(c.Provider,{value:n},e.children)}}}]);