Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement login for development only

Shulou Source: shulou.com Published: 2022-06-02 23:30:28 10月02日 Update

The main content of this article is "how to achieve login for development only", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to achieve login for development only"!

First add this route to the main () function.

Router.HandleFunc ("POST", "/ api/login", requireJSON (login)) login

This function handles POST requests to / api/login, where JSON body contains only the user name and returns the authenticated user, token, and expiration date in JSON format.

Func login (w http.ResponseWriter, r * http.Request) {if origin.Hostname ()! = "localhost" {http.NotFound (w, r) return} var input struct {Username string `json: "username" `} if err: = json.NewDecoder (r.Body). Decode (& input) Err! = nil {http.Error (w, err.Error (), http.StatusBadRequest) return} defer r.Body.Close () var user User if err: = db.QueryRowContext (r.Context (), `SELECT id, avatar_url FROM users WHERE username = $1`, input.Username). Scan (& user.ID, & user.AvatarURL,) Err = = sql.ErrNoRows {http.Error (w, "User not found", http.StatusNotFound) return} else if err! = nil {respondError (w, fmt.Errorf ("could not query user:% v", err)) return} user.Username = input.Username exp: = time.Now () .Add (jwtLifetime) token, err: = issueToken (user.ID, exp) if err! = nil {respondError (w) Fmt.Errorf ("could not create token:% v", err) return} respond (w, map [string] interface {} {"authUser": user, "token": token, "expiresAt": exp,}, http.StatusOK)}

First, it checks whether we are on the local host, or if the response is 404 Not Found. It decodes the body to skip validation because it is only for development purposes. The user with the given user name is then queried in the database, and if not, 404 NOT Found is returned. It then uses the user ID as the subject to issue a new JSON Web token.

Func issueToken (subject string, exp time.Time) (string, error) {token, err: = jwtSigner.Encode (jwt.Claims {Subject: subject, Expiration: json.Number (strconv.FormatInt (exp.Unix (), 10),}) if err! = nil {return ", err} return string (token), nil}

This function performs the same operation as above. I just moved it over to reuse the code.

After you create a token, it responds with the user, token, and expiration date.

Seed user

Now you can add the user you want to operate to the database.

INSERT INTO users (id, username) VALUES (1, 'john'), (2,' jane')

You can save it to a file and pipe it to Cockroach CLI.

Cat seed_users.sql | cockroach sql-- insecure-d messenger

okay. Once the code is deployed to the production environment and uses its own domain, the login feature will not be available.

At this point, I believe you have a deeper understanding of "how to achieve login for development only". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: User login development token function code content just data database date user name learning query practical deeper same subject subject interest Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information MariaDB vpn Huawei OPPO Reno