Skip to content

session #

Constants #

const default_session_name = 'VEXSESID'

the default session name :P

fn new_session_from_id #

fn new_session_from_id(id string, mut store Store) !Session

new_session_from_id genereates a new Session from the provided ID and storage medium.

fn new_session_id #

fn new_session_id() string

new_session_id generates a new sesssion id.

fn start #

fn start(req &ctx.Req, mut res ctx.Resp, opts SessionOptions) Session

start checks to see if existing session ID exists in cookies based off of of the SessionOptions.name field. Leaving this field empty will result in default session. It's highly suggested not to use the default for production builds. If no session ID exists, then an error will be printed to the console and a new session is instantiated and returned.

interface Store #

interface Store {
mut:
	read(string) !map[string]string
	write(string, map[string]string) !
	delete(string) !
}

Store is the interface that Session accepts to store data. Using an interface allows the user to create their own Store structure if the method of storage they wish to use doesn't exist.

struct LocalStore #

struct LocalStore {
pub mut:
	path          string = os.temp_dir() + '/vex_sessions.json'
	encode_pretty bool
}

LocalStore reads ands saves sessions on the local disk in a JSON-encoded format.

fn (LocalStore) read #

fn (ls LocalStore) read(id string) !map[string]string

read reads an individual session from the local session data.

fn (LocalStore) delete #

fn (mut ls LocalStore) delete(id string) !

delete removes an individual session from the local session data.

fn (LocalStore) write #

fn (mut ls LocalStore) write(id string, data map[string]string) !

write saves session data to the local storage.

struct Session #

struct Session {
	ctx.Cookie
mut:
	data map[string]string
pub mut:
	name    string    = 'default_' + session.default_session_name
	id      string    = new_session_id()
	expires time.Time = time.now().add_days(1)
	store   Store     = LocalStore{}
	res     ctx.Resp
__global:
	auto_write bool = true
}

Session contains user data, the session Store, and cookie information

fn (Session) set #

fn (mut s Session) set(key string, val string)

set sets a value in the session data

fn (Session) set_many #

fn (mut s Session) set_many(keyval ...string) !

set_many sets multiple keys and values in the session data

fn (Session) has #

fn (mut s Session) has(key string) bool

has checks if a value exists in the session data

fn (Session) pop #

fn (mut s Session) pop(key string) ?string

pop reads a values from the session data to return, then deletes the key-value-pair from the data.

fn (Session) get #

fn (s Session) get(key string) string

get reads a value from the session data and returns it. Returns an empty string if no key is found matching the one provided.

fn (Session) must_get #

fn (s Session) must_get(key string) ?string

must_get reads a value from the session data and return it. If key provided does not match any, then none will be returned.

fn (Session) remove #

fn (mut s Session) remove(key string)

remove deletes a key-value-pair from the session data.

fn (Session) is_empty #

fn (s Session) is_empty() bool

is_empty returns whether or not the session has any data stored in it.

fn (Session) regenerate #

fn (mut s Session) regenerate()

regenereate replaces the ID of the session, but leaves the data and cookie info the same.

fn (Session) write #

fn (mut s Session) write() bool

write saves the session data to the Store of the session

fn (Session) restore #

fn (mut s Session) restore() !

restore fetches the data from the Store of the session using the ID of the session

fn (Session) delete #

fn (mut s Session) delete()

delete deletes the session data from Store.

fn (Session) cookie #

fn (s Session) cookie() ctx.Cookie

cookie returns the Session as a ctx.Cookie instance.

fn (Session) set_header #

fn (mut s Session) set_header()

set_header sets a Set-Cookie on Session.res that represents the current Session

fn (Session) remove_header #

fn (mut s Session) remove_header()

remove_header removes the corresponding Set-Cookie header in Session.res.headers.

fn (Session) get_header #

fn (mut s Session) get_header() []string

get_header is a passthrough for Cookie.header_str.

struct SessionOptions #

struct SessionOptions {
mut:
	name      string    = 'default_'
	expires   time.Time = time.now().add_days(1)
	max_age   int
	path      string = '/'
	http_only bool
	secure    bool
	same_site ctx.SameSite = .lax
	store     Store        = LocalStore{}
}

SessionOptions are used to customize the 'Set-Cookie' header that will be sent with the response to the user when starting a session. It also contains the Store of a session.