<commit_before>// loginapp is an example web app with Login with Digits (phone number).
package main

import (
	"fmt"
	"github.com/dghubble/go-digits/digits"
	"github.com/dghubble/go-digits/login"
	"github.com/dghubble/sessions"
	"io/ioutil"
	"log"
	"net/http"
)

const (
	digitsConsumerKey = "YOUR_DIGITS_CONSUMER_KEY"
	sessionName       = "loginapp-session"
	sessionSecret     = "example cookie signing secret"
	sessionUserKey    = "digitsID"
)

// sessionStore encodes and decodes session data stored in signed cookies
var sessionStore = sessions.NewCookieStore([]byte(sessionSecret), nil)

// New returns a new ServeMux with app routes.
func New() *http.ServeMux {
	mux := http.NewServeMux()
	mux.HandleFunc("/", homeHandler)
	mux.Handle("/profile", requireLogin(http.HandlerFunc(profileHandler)))
	mux.HandleFunc("/logout", logoutHandler)

	// 1. Register a Digits WebHandler to receive Javascript login POST
	handlerConfig := &login.WebHandlerConfig{
		ConsumerKey: digitsConsumerKey,
		Success:     login.SuccessHandlerFunc(issueWebSession),
		Failure:     login.DefaultErrorHandler,
	}
	mux.Handle("/digits_login", login.NewWebHandler(handlerConfig))
	return mux
}

// issueWebSession issues a cookie session upon successful Digits login
func issueWebSession(w http.ResponseWriter, req *http.Request, account *digits.Account) {
	// 2. Implement a SuccessHandler to issue some form of session or write to db
	session := sessionStore.New(sessionName)
	session.Values[sessionUserKey] = account.ID
	session.Values["phoneNumber"] = account.PhoneNumber
	session.Save(w)
	http.Redirect(w, req, "/profile", http.StatusFound)
}

// homeHandler shows a login page or a user profile page if authenticated.
func homeHandler(w http.ResponseWriter, req *http.Request) {
	if req.URL.Path != "/" {
		http.NotFound(w, req)
		return
	}
	if isAuthenticated(req) {
		http.Redirect(w, req, "/profile", http.StatusFound)
	}
	page, _ := ioutil.ReadFile("home.html")
	fmt.Fprintf(w, string(page))
}

// profileHandler shows protected user content.
func profileHandler(w http.ResponseWriter, req *http.Request) {
	fmt.Fprint(w, `<p>You are logged in!</p><form action="/logout" method="post"><input type="submit" value="Logout"></form>`)
}

// logoutHandler destroys the session on POSTs and redirects to home.
func logoutHandler(w http.ResponseWriter, req *http.Request) {
	if req.Method == "POST" {
		sessionStore.Destroy(w, sessionStore.New(sessionName))
	}
	http.Redirect(w, req, "/", http.StatusFound)
}

// requireLogin redirects unauthenticated users to the login route.
func requireLogin(next http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, req *http.Request) {
		if !isAuthenticated(req) {
			http.Redirect(w, req, "/", http.StatusFound)
			return
		}
		next.ServeHTTP(w, req)
	}
	return http.HandlerFunc(fn)
}

// isAuthenticated returns true if the user has a signed session cookie.
func isAuthenticated(req *http.Request) bool {
	if _, err := sessionStore.Get(req, sessionName); err == nil {
		return true
	}
	return false
}
<commit_msg>Fix app.go login example, sessions Store.Destroy takes name<commit_after>// loginapp is an example web app with Login with Digits (phone number).
package main

import (
	"fmt"
	"github.com/dghubble/go-digits/digits"
	"github.com/dghubble/go-digits/login"
	"github.com/dghubble/sessions"
	"io/ioutil"
	"log"
	"net/http"
)

const (
	digitsConsumerKey = "YOUR_DIGITS_CONSUMER_KEY"
	sessionName       = "loginapp-session"
	sessionSecret     = "<PASSWORD>"
	sessionUserKey    = "digitsID"
)

// sessionStore encodes and decodes session data stored in signed cookies
var sessionStore = sessions.NewCookieStore([]byte(sessionSecret), nil)

// New returns a new ServeMux with app routes.
func New() *http.ServeMux {
	mux := http.NewServeMux()
	mux.HandleFunc("/", homeHandler)
	mux.Handle("/profile", requireLogin(http.HandlerFunc(profileHandler)))
	mux.HandleFunc("/logout", logoutHandler)

	// 1. Register a Digits WebHandler to receive Javascript login POST
	handlerConfig := &login.WebHandlerConfig{
		ConsumerKey: digitsConsumerKey,
		Success:     login.SuccessHandlerFunc(issueWebSession),
		Failure:     login.DefaultErrorHandler,
	}
	mux.Handle("/digits_login", login.NewWebHandler(handlerConfig))
	return mux
}

// issueWebSession issues a cookie session upon successful Digits login
func issueWebSession(w http.ResponseWriter, req *http.Request, account *digits.Account) {
	// 2. Implement a SuccessHandler to issue some form of session or write to db
	session := sessionStore.New(sessionName)
	session.Values[sessionUserKey] = account.ID
	session.Values["phoneNumber"] = account.PhoneNumber
	session.Save(w)
	http.Redirect(w, req, "/profile", http.StatusFound)
}

// homeHandler shows a login page or a user profile page if authenticated.
func homeHandler(w http.ResponseWriter, req *http.Request) {
	if req.URL.Path != "/" {
		http.NotFound(w, req)
		return
	}
	if isAuthenticated(req) {
		http.Redirect(w, req, "/profile", http.StatusFound)
	}
	page, _ := ioutil.ReadFile("home.html")
	fmt.Fprintf(w, string(page))
}

// profileHandler shows protected user content.
func profileHandler(w http.ResponseWriter, req *http.Request) {
	fmt.Fprint(w, `<p>You are logged in!</p><form action="/logout" method="post"><input type="submit" value="Logout"></form>`)
}

// logoutHandler destroys the session on POSTs and redirects to home.
func logoutHandler(w http.ResponseWriter, req *http.Request) {
	if req.Method == "POST" {
		sessionStore.Destroy(w, sessionName)
	}
	http.Redirect(w, req, "/", http.StatusFound)
}

// requireLogin redirects unauthenticated users to the login route.
func requireLogin(next http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, req *http.Request) {
		if !isAuthenticated(req) {
			http.Redirect(w, req, "/", http.StatusFound)
			return
		}
		next.ServeHTTP(w, req)
	}
	return http.HandlerFunc(fn)
}

// isAuthenticated returns true if the user has a signed session cookie.
func isAuthenticated(req *http.Request) bool {
	if _, err := sessionStore.Get(req, sessionName); err == nil {
		return true
	}
	return false
}

{'fraction_non_alphanumeric': 0.12256267409470752, 'fraction_numerical': 0.0006554153694904145, 'mean_word_length': 4.6940298507462686, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3179383', 'n_tokens_mistral': 2020, 'n_tokens_neox': 1959, 'n_words': 518}